tags:

views:

198

answers:

2

Steps to reproduce:

Create a TestAlias module in \WindowsPowerShell\Modules\TestAlias\TestAlias.psm1 with the following function and alias:

function foo
{ write-output 'foo' }


New-Alias -name bar -value foo

From Powershell a session

import-module TestAlias
bar

    The term 'bar' is not recognized as the name of a cmdlet, function, script file, or operable program...
+1  A: 

Use Export-ModuleMember in the PSM1 file to export the Alias

Export-ModuleMember -function foo -Alias bar

Ravikanth
+3  A: 

You can use

Export-ModuleMember -Function * -Alias * 

to export all functions and aliases.

By default, Windows PowerShell modules only export commands (functions or cmdletS), and not variables or aliases.

I'll go into a little more detail about why this is.

The short answers is that aliases, while convenient when writing one liners, are a barrier to understanding a script or a module. They are icing on the cake of a good cmdlet, but the core thing to expose is the good cmdlet. Aliases make it more difficult for a user reading your script to figure out what you're trying to do (Set-Content is a lot easier to understand than sc). Variables can be even worse to expose, as they can easily be set to unexpected values and as there is very little to help a user of your module figure out that they are there. Because commands are easily discoverable (Get-Command -Module FOO) and easier to explore (with Get-Help), the default that a module will export is only commands. As with most other things in PowerShell, you can override it if you choose, but by default commands are the only thing that are exported from a module.

Hope this Helps

Start-Automating
I agree with your point on aliases making scripts more difficult to understand. I dislike even seeing blog posts with % and ?. Normally I would not use aliases with modules, however I have an existing Powershell V1 function library I converted to a V2 module and I needed to change a few function names in order to support making some private. So that any old scripts based on the functions do not break I created alaises in the module.
Chad Miller