Windows PowerShell Get-Alias

Introduction to Windows PowerShell Get-Alias

PowerShell has a whole family of Aliases.  One group of these aliases provides shorthand for the regular cmdlets; instead of typing thirteen keystrokes for Get-Childitem, just use a PowerShell alias and type the three letters: gci.

However, I am going to concentrate on the other group of Aliases, those cmdlets that are designed to help people migrate from other languages.  Thanks to aliases, those who know DOS can still use dir, and those who are familiar with UNIX can still use 'ls' in PowerShell.  These old commands work because PowerShell has created an internal link so that when you type dir it translates to the cmdlet Get-ChildItem.

PowerShell Alias Topics

 ♣

A Mission For PowerShell Aliases – Abandon DOS

I have a tough mission, to persuade people to abandon the DOS command-line and switch to PowerShell.  There are three threads to this mission.

1) Have faith that an alias such as cd will change directory in PowerShell just as it did in DOS.

2) Realize that built-in operating system commands such as Ping, Ipconfig and Shutdown work in PowerShell 98% as well as they did with cmd.exe.  Consequently, there is no need to use that DOS box, just use the PowerShell command-line instead.  I understand that this is a leap of faith, and to begin with may take you out of your comfort zone.

3) The third aspect of the PowerShell alias cmdlets is how they deal with native operating system commands that need user input.  For this task we can employ start-Process, or the versatile PowerShell cmdlet New-Object with its -ComObject parameter.  I just want to make you aware that interactive command-line utilities, such as NetSh will work in PowerShell, however they need techniques outside the scope of this article.

In conclusion, my idea is that you will learn PowerShell faster if you use it for familiar DOS tasks.  Then gradually, it will become easier and more natural to start incorporating more and more pure PowerShell commands into your working habits.

Check Out PowerShell’s Get-Alias

In the old days I listed PowerShell’s aliases with this command:

# Research PowerShell Alias
Get-Command -commandType alias

Then I discovered that alias has its own cmdlet:

Get-Alias
# Get-Help Get-Alias -full
# Get-Alias -Definition Get-ChildItem

Note 1: The secret of success is employ the -Definition parameter.

Note 2: The benefit of the PowerShell Get-Alias is that you can investigate more properties by appending | Get-Member, thus:

# Research Get-Alias Properties
Get-Alias | Get-Member

Note 3: Get-Help Get-Alias  Now we have extra information try:
Get-Alias -definition Get-ChildItem

Note 4: The alias 'help' means you could try just:
Help Get-Alias.

Filter for built-in Aliases (They have the ReadOnly property)

#PowerShell Alias Example
Clear-Host
Get-Alias | Where-Object {$_.Options -Match "ReadOnly"}

Note 5: To list only the 50(ish) DOS and UNIX commands change -Match to -NotMatch

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

More Filters for the PowerShell Alias Family

There are other ways to filter Get-Alias, for example, [a-g]* lists all the aliases beginning with letters ‘a’ through ‘g’.  Incidentally, this simple example demonstrates how PowerShell employs wildcards, and also alerts you to the significance of different types of brackets.

As ever, you can refer to help, for example type: help Get-Alias.  There is also: help New-alias, or even help delete-alias.  I have deliberately not emphasised creating your own aliases.  My reasoning is this, any aliases you create will not work if you send scripts containing such aliases to other people.  This could cause confusion and thus is best avoided especially if you are just starting to learn PowerShell.

List of PowerShell’s Built-In Aliases

As you check this list see if you can detect two types of PowerShell Alias cmdlets, those convenience Aliases that simply save key strokes, for example, gci (Get-Childitem) and those Aliases that help people transition from other languages, for example, cd (Set-location).

®

Alias Definition Alias Definition
% Foreach-Object   ipal Import-alias
? Where-Object   ipcsv Import-csv
ac Add-Content kill Stop-process
cat Get-content lp Out-printer
cd Set-location ls Get-Childitem
chdir Set-location mi Move-Item
clc Clear-content mount New-drive
clear Clear-host move Move-Item
cli Clear-Item mp Move-property
clp Clear-property mv Move-Item
cls Clear-host nal New-alias
clv Clear-variable ndr New-drive
copy Copy-Item ni New-Item
cp Copy-Item nv New-variable
cpi Copy-Item oh Out-host
cpp Copy-property popd pop-location
cvpa convert-path ps Get-Process
del Remove-Item pushd push-location
dir Get-Childitem pwd Get-location
echo Write-object r Invoke-history
epal Export-alias rd Remove-Item
epcsv Export-csv rdr Remove-drive
erase Remove-Item ren Rename-Item
fc format-custom ri Remove-Item
fl format-List rm Remove-Item
foreach Foreach-object rmdir Remove-Item
ft Format-Table rni Rename-Item
fw format-wide rnp rename-property
gip Get-NetIPConfiguration rp Remove-property
gc Get-content rv Remove-variable
gci Get-Childitem rvpa Resolve-path
gcm Get-Command sal Set-alias
gdr Get-psdrive sasv start-Service
ghy Get-history sc Set-content
gi Get-Item select select-object
gl Get-location set Set-variable
gm Get-Member si Set-Item
gp Get-property sl Set-location
gps Get-Process sleep Start-sleep
group group-Object sort Sort-Object
gsv Get-Service sp Set-property
gu Get-unique spps Stop-process
gv Get-variable spsv Stop-Service
h Get-history sv Set-variable
history Get-history type Get-content
icm Invoke-command where Where-object
ihy Invoke-history  
ii Invoke-Item  
ilal Initialize-alias  

SolarWinds Response Time Viewer for WiresharkGuy Recommends: Response Time Viewer for Wireshark

Here is a free tool to troubleshoot network connection and latency problems.  Key concept: this is a free tool from SolarWinds that analyzes network packets captured by Wireshark (also a free tool).

When you inspect the data in the Response Time Dashboard, if you hover over an application such as Teredo or TCP, then you get an orange box showing a breakdown of network and application response times, note the 'Peak value' in addition to the 'Average'.

Download your free trial of SolarWinds Response Time Viewer for Wireshark

Create PowerShell Alias

The command to add your own PowerShell alias is: Set-alias.  You follow this command with your choice of aliasname and then complete the instruction by specifying an existing PowerShell Verb-Noun, for example:

# PowerShell create alias
Clear-Host
New-alias xcopy Copy-Item
# or
Set-Alias xcopy Copy-Item # works just as well

The new alias is called xcopy and what it does is the equivalent of Copy-Item.  To double check type: Get-Alias xcopy

N.B. Don’t go mad with aliases, stick to one verb and one noun. I tried this:
Set-alias eventvwr Get-Eventlog application, however it did not work as I had hoped – too many arguments.
Set-alias eventvwr  is ok, but rather disappointing because it prompts you for the name of the log.

Function – An alternative to Alias
If you really need a more complex set of commands then consider creating a function, which you can then save to the Function ‘drive’, investigate with:  Get-psdrive or Get-psdrive function.

Sequencing – Aliases come first
Suppose you have an Alias called ‘eventvwr’ and also a cmdlet with the same name ‘eventvwr.ps1’, but with different instructions.  What would happen is the Alias would be processed first.  In fact PowerShell checks the Aliases before it looks for functions or cmdlets.

How to Permanently Save Your PowerShell Alias

If you just create an alias at the command prompt it is desperately disappointing that PowerShell does not remember your aliases the next time you logon.  Good news, you can add your aliases to profile.ps1, the benefit is they will now be available for each and every session.

My friend ‘Barking’ Eddie even has a file just for his aliases, he calls it profile_alias.ps1.  His trick is to call this dedicated file from within profile.ps1 with this line:

."C:\ Documents and Settings\EDDIE\My Documents\PSConfiguration\profile_alias.ps1"

If you try this at home, then substitute your username for EDDIE, or else it will not work.
N.B. That’s not dirt on the screen or a Guy error, there really is a full stop or ‘period’ in front of ."C: \ ".

Some say Eddie is Barking mad, but actually he is from Barking in Essex, either way, he takes this linking idea further and creates a profile_function.ps1 file for his special functions.

Aliases in cmdlets?
Can you call an Alias from a cmdlet?  The answer is yes, why ever not, remember that cmdlets mimic keystrokes you type at in the Microsoft Shell.

Guy Recommends:  SolarWinds’ Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

Problems Caused by Custom PowerShell Aliases

My old friend ‘Barking’ Eddie has produced some great PowerShell scripts and at present they work fine.  However, Eddie has created a time bomb, the problem is that his cmdlets have so many bizarre aliases that no-one else can understand them.  Eddie has no scruples, he is hoping that the people who employ him today, will pay him big bucks to come out of his retirement and fix those cmdlets that no-one else can decipher.

The lesson is this, if you only have yourself to please, then create all the aliases that you want.  However, if others need to understand or troubleshoot your scripts, then filling them with aliases will only store up problems, which will return to haunt you.

See Guy’s Top 10 PowerShell Aliases »

Summary of PowerShell Alias

Knowing about PowerShell Aliases has these three benefits:

  1. To reduce typing.
  2. To smooth the transition from cmd.exe or other scripting languages to PowerShell.
  3. To create your own shortcuts for commands that you use often.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials   • Introduction   • PowerShell 3 New Aliases   • -Online   • 3 Key Commands

Top 10 PowerShell Aliases   • PowerShell Alias   • PowerShell $_ Variable   • Free CSV Import Tool

PowerShell Parameters   • Cmdlet scripts   • Vista PowerShell   • Windows 8 PowerShell 3.0

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.