Creating Aliases in PowerShell

Aliases come in handy when the length of commands become a bit tedious. It acts as a second name to whatever command you designate to it. In this tutorial, you will learn how to create an alias in PowerShell, let's get started!

Setup

If you have not already done so, click open Windows PowerShell ISE. Think of two programs that you use the most. For the purpose of writing this tutorial, notepad and internet explorer are selected.

Step one.

Usually people use the shortcut where they search for the program in the start menu. We can actually set notepad to np and calculator to cl. The plan is to create these new aliases in which np and cl will be typed in rather than the full names of the programs.

This is easily accomplished by running the following:

Example

New-Item alias: np –value c:\windows\system32\notepad.exe
 
New-Item alias: cl –value c:\windows\system32\calc.exe 
    

Copy and Try it

This particular method also works for PSH commands, such as Set-Execution. If you would like to set up an alias for this as well, you would simply run:

Example

New-Item alias: se –value Set-Execution
    

Copy and Try it

Step two.

The only time you need to set the full path as presented in the first two aliases, is when you are pointing to an external command such as an application. Also, if you want an alias to refer to something different and not Set-Execution, you don't have to delete it. You can just redefine it using the Set-Item command. Let's change se to run a new, made-up command called Show-Monkeys:

Example

Set-Item alias: se –value Show-Monkeys
    

Copy and Try it

You can also define numerous scope options when creating a new alias. A scope is just a definition for where an item can be accessed. The scope options are:

  • None: A regular alias that you can use and delete when you want to.
  • Constant: Constant aliases can't be deleted, nor have changed values during the session.
  • ReadOnly: ReadOnly aliases are like constant aliases, but can be deleted and have its values changed, provided one specifies the Force parameter when it is changed or deleted.
  • Private: private scoped aliases can be seen only with the current scope.
  • AllScope: AllScope is visible across all new scopes that are created.

You can combine options as well. If you want to make np alias ReadOnly while the cl alias Constant, with its scope set to AllScope, you can run this:

Example

New-Item alias: cl –value C:\windows\system32\calc.exe –options "AllScope, ��
 
New-Item alias: np –value C:\windows\system32\notepad.exe –options "ReadOnly"
    

Copy and Try it

Step three.

You can also use Set-Item to set the options for an alias after it has been created. You can rename the np alias to note using the following command sequence:

Example

	
Rename-ItemR  alias: np –newname note
    

Copy and Try it

Another way to create and update aliases is by using the New-Alias and Set-Alias commands. These are more straightforward than New-Item. You can create a new alias as follows:

Example

	
New-AliasN  np c:\windows\system32\notepad.exe
    

Copy and Try it

Any alias you create during your PSH session will only be valid for that given instance of PSH of course. Once you close the window, the aliases defined will no longer exist.

Remarks last but not least…

Hopefully this gave you a basic understanding of the usefulness and simplicity of creating aliases. Just keep in mind that in PowerShell, anything is possible! Join us next time for additional Windows PowerShell tutorials! Till then…