Creating Persistent Aliases in PowerShell

I am sure creating temporary aliases has been fun and all, but let us get more usefulness out of them by making them permanently available! In this tutorial, you will learn how to create persistent aliases.

Setup

If you have not already done so, click open Windows PowerShell ISE.

The Concept…

There really is no point in making something temporary unless it was meant to be temporary. Making a permanent alias is helpful and far more efficient. The best part about it is that all of this can be done by defining aliases in your profile script. Since the profile is executed every time you open a new PSH window, it's the perfect location to define your aliases of choice so they're immediately available to you the moment you open up PSH.

There are two commands that PSH has that will assist in making the aliases persistent. They are called Export-Alias (exports the alias information to a file) and Import-Alias. You can define the aliases on one system, export the alias definition and then distribute it to all the other systems.

Step one.

To export and import your aliases, run the following command sequence:

Example

Export-Alias c:\thealiases.txt
 
Import-Alias c:\thealiases.txt

    

Copy and Try it

Now, as a side note, by default the Export-Alias command overwrites the contents of a file it automatically created. If you wish to not overwrite, you can implement the noclobber parameter so the command returns an error if the file already exists.

The command is implemented as follows:

Example

Export-Alias c:\thealiases.txt –noclobber
    

Copy and Try it

Also, there is the option to append to an existing alias file. This feature is great if you have aliases defined in various locations and are trying to consolidate them into a single file. This is done as follows:

Example

Export-Alias c:\thealiases.txt -append
    

Copy and Try it

Remarks last but not least…

So there you have it! Permanent aliases, and once again this is done in your PSH profile! It definitely comes into great use if everything you do on your computer is consistent daily. Join us next time for additional Windows PowerShell tutorials! Till then…