Friday, November 20, 2009

Simple discoverability function for PowerShell

Sometimes it's easy to forget the little things that have made my life with PowerShell much better and more efficient. PowerShell's verb-noun command structure is great, but when I'm typing away in the shell I prefer brevity. While the command 'Get-Alias' might sounds like it would tell you what alias(es) are available for a particular command that you're tired of typing the full name of, it doesn't. So I created a quick little one-liner in my user profile:

function Get-Aliases ($cmd) { Get-Alias | where { $_.Definition -eq $cmd } }

This simply gets all the defined aliases and compares the definition of each alias to the command you specify.  Any alias that matches is written to output.  But since 'Get-Aliases' can be a bit long I use an alias for it:

Set-Alias gals Get-Aliases

And that's it!  Now we can run:

PS> gals get-command

CommandType  Name  Definition
-----------  ----  ----------
Alias        gcm   get-command

Little shortcuts like learning aliases can go a long way to improving your experience.  Have fun with them!