Expanding aliases in PowerShell ISE or any PowerShell file

393790664_da5b0ddb12_m
Further extending my PowerShell ISE module (ISEUtils) I’ve added a function to convert aliases either in the currently active ISE file or (in case a a path is provided) within any PowerShell file (that way the function can be also used from the PowerShell Console) to their respective definitions.
Aliases are very useful when working interactively, since they help saving extra keystrokes when you just want to get things done fast. At the same time if we are speaking about production code where readability, and easy comprehension of the code are much more important the usage of aliases should be avoided ( read here for a good article on best practices for PowerShell alias usage).
With the Expand-Alias function you can get the best of both worlds. Writing clearer code while avoiding extraneous keystrokes. For the code samples in my blog posts I’m also using aliases quite a lot, but would like to start using the new function from now on.
Below is the source code for Expand-Alias:

Usage:

  1. If the function is called without any parameter it will expand all aliases within the current ISE file.
  2. Providing a full-path to the path parameter will expand all aliases within the respective PowerShell file instead.

Adding the following lines to your PowerShell profile (you can read here and here on how to work with profiles) will automatically load the function and add an item to the AddOn menu to call it on every ISE startup:

$FULLPATHTOEXPANDALIAS = 'c:\expand-alias.ps1'
. $FULLPATHTOEXPANDALIAS
$null  = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add('Expand-Alias', { Expand-Alias }, 'CTRL+SHIFT+X')

shareThoughts


Photo Credit: Martin Gommel via Compfight cc

Advertisements

I'd love to hear what you think

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s