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!
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.
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
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:
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:
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:
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"
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:
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:
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.
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…