The Mystery of the PowerShell “alias”

So… I have a situation, and I’m not really sure what’s going on.  I think this is a two-fold problem:

Problem 1: Variable and Alias – Odd Things – There’s a Ghost in the (Power)Shell

To set the picture, I’m running Windows PowerShell 5.0 on Windows 10, but we also saw this on Windows PowerShell 2.0 in Windows Server 2008 R2.  When I type alias, it outputs contents as if it were running Get-Alias.

alias1-20151103

My instinct tells me this should be an alias.  So let’s see:

alias5-20151103

Nope… not an alias.  Is this a variable?

alias2-20151103

Nope… not a variable.  Is this a command?

alias3-20151103

Nope…. not a command. Is this a function?

alias4-20151103

Nope.  Is this an executable that cmd can run?

cmdexe-20151103

Nope.  We also see this phantom listing (for lack of a better term) with variable.

So… problem 1 – what are these things if they aren’t commands, aliases, variables, or  functions?  June and I tested these with PowerShell v2.0 on Windows Server 2008 R2 and PowerShell v5.0 on Windows 10, natively.  We also tested it in versions 2.0, 3.0, 4.0, and 5.0 side-by-side.

Problem 2. The Shadow of the Phantom (not the Opera)

So piggybacking off of #1, I have an odder situation.  I have a custom PS module that I’ve created for teaching – originally used in 2011 and repurposed for Coding 101 this past October.  June changed my line #1 to include -Name and -Value (but this doesn’t make a difference).  I have an issue with the alias scenario above, coming from a Linux background.  The command alias by itself is used for creating aliases, so I like to fix the alias with the proper usage:

alias6-20151103

PowerShell is not auto-loading this module, but we can load it with Import-Module and the alias alias works as expected.

Is our phantom friend from #1 shadowing my module, preventing it from auto-loading?  If so, is this a bug?  Or is this possibly an undocumented feature?  (What…?!? I’m a dev. I had to put it that way. 🙂 )

UPDATE – The Mysterious 2nd Pass

So I asked about this on Twitter, and I got responses from the PowerShell community – thanks to Bruce Payette, Boe Prox, and Lee Holmes.  Basically, as Bruce Payette explained it, since PowerShell v1.0, when a command is executed, PowerShell does a 2-pass find:  first it looks for the command by name, and if that fails, then it does a second pass with Get- prepended to it.  So…

  1. alias is looked for but not found.
  2. Get-Alias is looked for, found, and executed.

Boe Prox  blogged about this behavior and how he sniffed it out with the Trace-Command cmdlet.

Lee Holmes explained that auto-loading a module happens if CommandNotFound is about to get thrown.  However, because of this second pass, CommandNotFound doesn’t get thrown, so the module does not auto-load.

And this… this is why I love the PowerShell community.  They’re super helpful and can shed light on issues even with 140 character limits on Twitter. 🙂  Thanks, guys!

Read an E-Book Week – Wiley Makes Me Smiley

Disclaimer: Although my work is related to the topic at hand, these are the thoughts of Sarah, the book junkie, and are not necessarily those of my employer.

As a developer, I’ve always been a fan of those books with the red covers and the yellow text along the spine. Those would be the books from Wrox, an imprint of John Wiley & Sons, Ltd. When I wasn’t looking into O’Reilly titles, Wrox was the second publisher I’d go to. The more I settled into the development realm, Wrox became the first that I turned to.

In my day job as a web developer, I like having Wrox books on my desk as a reference, especially Professional C# 4.0 and .NET 4. While my physical bookshelf at work has all sorts of reference books, I also like that Wrox has eBooks available, not just at their store but also through public libraries and other retailers. This means that I can check out a book online and have it on my desktop at work as a reference for a couple weeks at a time (provided there isn’t a queue ahead of me). If there’s high enough of a demand and my library doesn’t stock the book well, then I could consider purchasing the eBook.

Besides being able to check Wrox books out at the library, the other thing that makes me smiley about Wiley is their Sybex imprint. When it comes to IT imprints, Sybex is the first I think of – their “Mastering” and “Instant Reference” books are great references to have. Sybex is also my publisher and has been a great company to work with. So of course they make me smiley!

These are just more technical publishers that I simply adore. Tomorrow, I’ll talk about a couple online services I have access to that get me my technical eBooks when I need a quick reference and know that these services reliably carry them. Stay tuned!