How To Create a PowerShell Alias

Ad

When you use a command often, you may want to be able to access that PowerShell cmdlet more quickly from the console.  There are many aliases that PowerShell recognizes by default, but if you want to create your own, you can save yourself a lot of keystrokes.

FIND OUT:  Which Cmdlet’s Don’t Have An Alias Already?

 

When you’re ready to make an alias, it’s really easy to add it.  But you might want to consider some of the common conventions in use for aliases if you’re going to keep your alias in use for multiple sessions.

Ad

Get To Know The New-Alias Cmdlet:

The New-Alias cmdlet takes these parameters

Parameter Name Position Required Default Accept Pipeline What it does
Name 1 Yes   By PropertyName This is what you’ll type to call the alias.
Value 2 Yes   By PropertyName This is the result of the alias – the command that it’s calling
Description Named No   No A description of the alias
Force (switch) Named No   No If the alias already exists, it makes this command update (change) the existing alias
Option Named No None No You can choose None (default), ReadOnly (can’t be changed except with “force”), Constant (can’t be changed at all), Private (can only be seen in the scope listed by the “scope” parameter)
Scope Named No   No You can set the scope to be Global (all parts of the session), Local (The current part of the session), or Script (all parts of the current script)
Passthru (switch) Named No   No When the new alias command is run, the alias is returned.  Passthru allows the results (the alias object) to be passed through the pipeline.
Confirm
(switch)
Named No   No Prompt for confirmation before creating the alias.
WhatIf (switch) Named No   No Doesn’t create the alias, just tells what would happen if it tried to create the alias

Notice that there are two required parameters: Name and Value.  All we have to do to create an alias is populate those:

New-Alias –name ip –value ipconfig

But there is still an easier way.

Ad
Find Tech Jobs on Dice!

Creating an Alias in PowerShell with Shortcuts

First, there is an alias for the New-Alias cmdlet:  “nal”

Second, the required “name” and “value” are positional, which means they don’t have to be named.  They can be assigned to the right parameter by putting them in their correct order.  This makes creating our alias even easier:

nal ip ipconfig

Save Your Aliases Between PowerShell Sessions

If you want to save your aliases, you’ve got two options: Either write the alias into your profile, or export the aliases using Export-Alias.

In situations like an I.T. department wants to all use the same custom aliases, or you’ve got want to use the same custom aliases on all of your machines, you should export the aliases.

Either way, you will need to create a profile before you can either define aliases or import them into new sessions

Ad
CategoriesUncategorized