Fast & Furious PowerShell: Unmask the alias

I’ve started using a lot of aliases in my PowerShell work. Many of them defined in my PowerShell profile. Unfortunately, I don’t always remember what I’ve defined, or even what the alias sometimes stands for. But with a few fast & furious PowerShell expressions, I can strip away the mystery.

Aliases are managed by the Alias provider. In PowerShell, that means I can use Get-ChildItem (or the DIR alias) and explore the alias provider just like a file system. Try this and you’ll see all the aliases on your system:

dir alias: | format-table -auto

I like to pipe output to format-table so I can see more of the definition.

Want to see more detail about a specific alias?

PS E:\ > dir-alias wd |format-list

Name : wd
CommandType : Alias
Definition : E:\Program Files\Microsoft Office\Office12\winword.exe
ReferencedCommand : winword.exe
ResolvedCommand : winword.exe

This is my shortcut to start Microsoft Word. The data is essentially the same thing you get by using Get-Alias.

But what about an alias for a custom function? Works the same. I’ll use Get-Alias because it makes more sense:

PS E:\ > get-alias du | format-table -auto

CommandType Name Definition
—————- —— —————
Alias       du   Get-DiskUsage

But what is Get-DiskUsage? Easy enough to find.  I need to check the ResolvedCommand property:

PS E:\ > (get-alias du).ResolvedCommand | format-table -auto

CommandType Name          Definition
—————- ——          —————
Function    Get-DiskUsage param( [string]$computer = “.” , [switch]$all) …

If I want to see the entire definition:

PS E:\ > ((get-alias du).ResolvedCommand).Definition
param( [string]$computer = “.” , [switch]$all) $size = @{ l = “Size (MB)” e = { $_.size/1mb}; f = “{0:N}”}
$free = @{ l = “free (MB)” e = { $_.freespace/1mb}; f = “{0:N}”}
$perc = @{ l = “percent” e = { 100.0 * ([double]$_.freespace/[double]$_.size)}; f=”{0:f}” }
$name = @{ e = “name” f = “{0,-20}” }
$fields = $name,$size,$free,$perc

# in case the user wants to see more than just local drives
$filter = “DriveType = ‘3’”
if ( $all ) { $filter = “” }

# go do the work by getting the information from the appropriate
# computer, and send it to format-table with the appropriate
# fields and formatting info
get-wmiobject -class win32_logicaldisk -filter $filter -comp $computer |
format-table $fields -auto

The formatting may not be exactly right from copying and pasting but you get the idea. So, to wrap up, use this:

dir alias: | select name,definition | format-table -auto

To get fast look at all your aliases. Finally, try this:

PS E:\ > foreach ($item in (dir alias:)) {write-host -back yellow -fore blue $item.name ; ((get-alias $item.name).ResolvedCommand).Definition}

By the way, instead of using MORE, you can click your mouse in the PowerShell console window and that should pause scrolling. Tap the space bar to resume. Send this output to a text file and you have a nice alias backup.