PowerShell

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



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 alias cmdlets which 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.  This week 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 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 2: Get-Help Get-Alias (Or use the alias 'help' and try plain: 'help Get-Alias').  Now we have extra information try:
Get-alias -definition Get-ChildItem

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

#PowerShell Alias Example
Clear-Host
Get-Alias | where-object {$_.Options -match "ReadOnly"}

Note 1: To list only the DOS and UNIX commands change -match to -notMatch

Note 2: 'Where' is an alias for the cmdlet 'where-Object'.  Check for more 'object' aliases with:
Get-alias | where-object {$_.definition -match "Object"}

More Filters for the PowerShell Alias Family

You can also 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.

Guy Recommends:  A Free Trial of the Orion Network Performance Monitor (NPM) v10Review of Orion NPM v10

Solarwinds' Orion performance monitor will help you discover what's happening on your network.  Also this utility will guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.  Because it produces network-centric views, the NPM is intuitive to navigate, and you can export the results to Microsoft Visio.

Perhaps Orion's best feature is the way it suggests solutions.  Moreover, if problems arise out of the blue, then you can configure Orion NPM v10 to notify members of your team what's changed and how to fix it.

If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of Solarwinds' offer and download a free trial of Orion's Network Performance Monitor.

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
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
gal Get-alias 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    
ipal import-alias    
ipcsv import-csv    
  ˚

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.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teach me more about how the system itself operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

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.

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.

Summary of PowerShell Alias

As far as I can see, using a PowerShell Alias 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.

See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Introduction   • Vista PowerShell   • Windows 7 PowerShell 2.0

PowerShell Parameters  • -Online  • PowerShell Alias  • 3 Key Commands  • Cmdlet scripts

Please write in 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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

WebThis Site

Guy Recommends: WMI Monitor and Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

 Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.