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!

PowerShell-Specific Providers, a Followup to 4/26

If you were sitting in my presentation last Tuesday, you may have caught my comment on some of the PowerShell-specific providers (Variable, Alias, Function) and how I haven’t really seen much use of them. While watching Allen White present on PowerShell and Policy Management in SQL 2008, I had a thought about this. I thought of a use for these providers What if you wanted to check to see if an alias existed before creating it? Same question for variables and functions…

We have the Test-Path cmdlet, so why not use Test-Path with these providers to see if the items exist before creating them?

While these are fairly primitive, they can work:

function Test-Alias($aliasname){
    return Test-Path "alias:$aliasname"
}

function Test-Function($functionname){
    return Test-Path "function:$functionname"
}

function Test-Variable($variablename){
    return Test-Path "variable:$variablename"
}

Oh the random PowerShell thoughts that zip by…

This One Time in a PowerShell Session…Part 3 of 31: Where Did All the Good Cmdlets Go?

Yesterday, I mentioned the Get-Verb cmdlet as a good one to know if you’re going to make your own cmdlets. Today, Matt is talking about all the good cmdlets. Really, he’s just getting your feet wet with a few cmdlets… our favorite cmdlets will come out throughout this series.

If you have any questions throughout this series, definitely leave us comments on our posts or email us. Matt and I love feedback, and despite this being a 31 days series, we may not like what we have picked for our topic of the day and may replace our pre-selected topic with something you suggest! You can email me at sarah at codinggeekette dot com.

This One Time in a PowerShell Session…Part 2 of 31: The Basics on How to Read PowerShell

Yesterday, I mentioned that I liked the Verb-Noun naming of PowerShell commands. Today, Matt is writing about The Basics on How to Read PowerShell, which explains these nouns and verbs. He’ll cover some quirks with them. He also talks about case sensitivity.

I only have one other tidbit to share. Matt mentions the verbs that are used, but he doesn’t tell you how he got those verbs. In PowerShell, there’s a Get-Verb cmdlet that lists the approved verbs. If you find yourself writing cmdlets (which we’ll talk about later), then you’ll want to remember this cmdlet.

PowerShell Not Your Father’s Command Line…

Throughout this month, my amazing coauthor Matt Hester and I will be taking you on a journey through our favorite tool at the moment – PowerShell! We’ll cover some things that we talk about in our book that’s due out May 16th, in addition to some things that aren’t in the book but still make for great points. (And yes, we’re taking a page out of Jeff Blankenburg‘s book and writing a “31 Days” series.)

Originally, Matt titled the series “PowerShell Not Your Father’s Command Line”, but he caught wind of one of my goofy Facebook statuses (This one time, at a command prompt…) and I suggested we rename it to “This One Time in a PowerShell Session…”.

Edit on 5/3 – Looks like Matt’s IT influencers voted on the previous series name, so they’re going back to the Not Your Father’s title.

I will try to keep this list updated as we post our days. We hope you enjoy our series!

Get-Popcorn. Get-Soda. Get-Chair. Read-Series!