Using PowerShell Aliases in a Module
Creating your own aliases in PowerShell is pretty straightforward. The New-Alias cmdlet allows you to create handy shortcuts for existing cmdlets or functions you have created yourself.
I write a lot of functions for my own modules, so having shortcuts for some of these functions would be pretty useful when using them via a module. However, when I first added some to one of my modules they weren’t available for use. Let’s have a look at an example to see what you need to do to make them available.
We have a module TestModule containing one function and one alias.
The contents of the module are:
function Write-HelloWorld {
Write-Host "Hello World" }
New-Alias -Name HelloWorld -Value Write-HelloWorld
However, when I import the module the function is available, but the alias is not.
Import-Module TestModule Write-HelloWorld HelloWorld
To make the alias available, we need to add the following line to the module file , since by default a module will only export functions.
Export-ModuleMember -Alias \* -Function \*
Now if we re-import the module (note we need to use the -Force parameter since the module is already loaded) the HelloWorld alias is available.
Import-Module TestModule -Force HelloWorld