Weekend Scripter: Looking at PowerShell Aliases

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about looking at Windows PowerShell defined aliases.

Microsoft Scripting Guy, Ed Wilson, is here. I am constantly amazed by Windows PowerShell. It keeps getting better and better. There are so many improvements in each new version that it is hard to keep up with them. For example, this morning I was playing around with Windows PowerShell and sipping a cup of English Breakfast tea. Just basic tea, nothing extra. I was looking at aliases, and I got this idea. I would like to load all of the modules, look at each module, and see what aliases are exported. Groovy.

So first I loaded all of the modules. I actually used my IAM function to do this. (For more information, see the Hey, Scripting Guy! Blog, Add a Couple of Functions to PowerShell Profile.)

Get-Module -ListAvailable | Import-Module

Now, I decide to find the exported aliases from each of the modules. To do this, I again use Get-ModuleListAvailable and send it to the Foreach-Object cmdlet. Then I gather the exported aliases from each module (gmo is an alias for Get-Module, and % is an alias for Foreach-Object). The script is shown here:

gmo -l | % {(get-module $_.name).exportedAliases}

I look at the output, and I can see that the command returns a dictionary object with keys and values. The key and the value are exactly the same, as shown here:

Image of command output

Because I know that a dictionary object has a property named Keys that returns a collection of keys, I group my previous expression, and call the Keys property. This command is shown here:

(gmo -l | % {(get-module $_.name).exportedAliases}).keys

It works exactly as I planned. Now I can use the Foreach-Object cmdlet and retrieve the specific item from the Alias ps drive. This command is shown here:

(gmo -l | % {(get-module $_.name).exportedAliases}).keys | % {get-item alias:$_}

Following is the command and the output.

Image of command output

Hmmm. I look at the output, and it seems familiar. It looks like the standard output from the Get-Alias cmdlet. So I type the Get-Alias cmdlet into my Windows PowerShell console, and press ENTER. Yep, the output looks the same:

Get-Alias

Image of command output

Except, as I look over the output, I can see that there are some blanks. I was specifically looking for aliases defined in modules, and some of the aliases, such as Clear-Host and Clear-History, are not defined in a module, but rather, they are part of core Windows PowerShell. I verify this by storing the results into variables and looking at the count. The commands are shown here:

PS C:\> $a = Get-Alias | select name

PS C:\> $b = (gmo -l | % {(get-module $_.name).exportedAliases}).keys | % {get-item a

lias:$_} | select name

PS C:\> $a.count

259

PS C:\> $b.Count

145

So, there are 114 aliases that are canonical to Windows PowerShell. I decide to compare the objects and verify this as shown here:

Compare-Object -ReferenceObject $b -DifferenceObject $a

Here is the command and its associated output:

Image of command output

 I can also retrieve aliases that do not report a module name via Get-Alias. This technique is shown here:

Get-Alias | where {-not ($_.module)}

Well, that is enough playing around for one day. Join me tomorrow for more Windows PowerShell fun.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon