Get-Command, Aliases, and a Bug

I stumbled across some interesting behavior the other day as I was demonstrating something that I understand pretty well.

[Side note…this is a great way to find out things that you don’t know…confidently explain how something works, and demo it.]

I was asked to give an overview of how modules work in PowerShell. I’ve been writing and using modules since PowerShell 2.0 came out (2009?) so I didn’t think there was anything (at least anything basic) that I wasn’t comfortable with. Not to say that there aren’t module concepts I’m not super-clear on, but the basics should have been all worked out.

After explaining the concepts of modules (encapsulating functions, variables, aliases) and showing how PowerShell knows where to look for modules, I turned to an example module I had written.

I won’t replicate that module here, because the contents don’t really matter. I’ve boiled the “weirdness” into a simple example and it looks like this:

function Get-Thing{

new-alias -Name MyDir -Value Get-ChildItem
new-alias -Name Func  -Value Get-Thing
new-alias -Name File -Value Get-Item
new-alias -Name Get-TheThing -Value Get-Thing
Export-ModuleMember -Function * -Alias *

If you save that as SampleModule.psm1 file (and put it in a same-named folder in the PSModulePath), you will be able to play along with me.

I showed the group that Import-Module was able to import the module using the name only, not requiring the user to know what path the module was installed into. Then, I thought “I’ll show them how to use Get-Command to find the items that were imported from the module!”

Get-Command -module SampleModule

Imagine my surprise when I saw the following output:

Only one of the three aliases showed up.

Further qualifying the Get-Command by specifying the CommandType (which should logically show fewer results) showed all three.

As a side note, I was also able to see the other aliases by using the -All switch, even though the help for -All says it is used for showing commands that are hidden due to naming collisions.

This isn’t a huge thing, but I did go ahead and add it to the PowerShell User Voice here. I’ve reproduced it in 5.0 and 5.1. I wouldn’t be surprised if it has been this way for a while.

What do you think? Surprised by this result?

Let me know in the comments.


One Comment

  1. Pingback: Get-Command, Aliases, and a Bug - How to Code .NET

Comments are closed.