Ezine 172 – Getting Started with Microsoft PowerShell

Ezine 172 – Getting Started with Microsoft PowerShell

I have designed this week’s ezine for those who are new to PowerShell.  It is also aimed at those who have had one flirtation with this scripting language, and are now returning for a second romance.  For those who have followed my previous scripts I say to them, ‘Focus on the Learning Notes in the examples below’.

Topics for Getting Started with PowerShell

This Week’s Secret – Seeking Help

Even though Microsoft make their help files comprehensive, even though the internet has a vast source of information, people are still reluctant to take advantage of this help.  I am convinced that the root cause of this avoidance is now psychological rather than any inadequacy of the help files.  Asking for help seems to have a stigma, particularly for men.  Perhaps my expressing this view will spur people to take the contrary view, and as a result they will seek help at every opportunity – just to prove me wrong!

With learning PowerShell I have performed the mental trick of ‘seeking assistance’ rather than ‘pleading for help’.  Here are five techniques that served me well in getting started with PowerShell, this week I will concentrate on item #2. get-Command.

  1. Read the screen.  Examine each PowerShell error message, in particular, note the line number.
  2. get-Command.  What cmdlets are out there?
  3. get-Help verb-Noun -full.  This gives detailed assistance with the parameters, and good examples of the cmdlet in use.
  4. get-Member.  This displays properties which we can then employ in making better scripts.
  5. get-Help about*  Please note there is a little known family of help files beginning with ‘About_’.

This Week’s Mission

This week I want to feature a technique which lists all the built-in PowerShell cmdlets.  From this simple beginning, we will move on to investigating processes currently running on your computer.  I have twin aims of introducing you to get-Command, and then doing some real work with get-Process.

Preparation: Install Microsoft PowerShell

Overview
I am assuming that you have installed PowerShell, and you are sitting at the PowerShell prompt.  If you don’t yet have PowerShell then downloaded the appropriate version of PowerShell and of .Net Framework from Microsoft’s site.

Detailed instructions
For detailed instruction on getting PowerShell up and running see the previous ezine.

Example 1: get-Command

Always remember that each PowerShell cmdlet consist of a verb-Noun couplet.  All that get-Command does is let you know which PowerShell cmdlets exist and are available.  A simple reason for researching with this command is that I cannot remember the names of all the objects that go with the ‘get’ verb.  A more advanced reason for employing get-Command, I don’t know what commands are available after I add a PowerShell snap-in such as QAD cmdlets.

For a basic form of this example, just type these three words:
get-Command get*

# PowerShell script to research Microsoft PowerShell commands
# Author: Guy Thomas
# Version 1.2 September 2008 tested on PowerShell v 1.0

$Research = get-Command get*
$Research
"There are " + $Research.count + " commands beginning with get"

Learning Points

Note 1:  Introducing the variable $Research is not strictly necessary, however, it increases our capability and knowledge by exposing the .count property.

Note 2:  Observe how flexible the plus sign is.  Not only does (+) add numbers, but also it joins text strings.  Incidentally, I once spent ages looking for PowerShell’s concatenator, when all I needed was (+).

Note 3: ‘Get’ is the default action of every PowerShell cmdlet.  By that I mean you could try plain ‘Command’ instead of ‘get-Command’.  It is also no surprise that you see more cmdlets beginning with ‘get’ than other verb.  Furthermore, ‘get’ is safer to experiment with than ‘set’, ‘remove’, or ‘stop’.

Challenge:  Seek out other PowerShell commands, for instance instead of get*, substitute: start*, new*, or any of the verbs mentioned in Note 3.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Example 2a: get-Process

Please excuse the jump from help techniques with get-Command to get-Process, but I am keen to show you scripts that perform real tasks.  One theme that I keep revisiting is how PowerShell scripts mimic what you can do with a GUI.  Take every opportunity to compare the GUI with the script, and thus discover a little more about the operating system, while you learn about PowerShell.

Scripts are usually superior to the GUI particularly when it comes to repetitive tasks.  In the case of the Task Manager and its Process tab, you can also do stuff with PowerShell that I have not found a way of achieving with the GUI.  This script groups ‘Processes’ by their PriorityClass.  Thus any unknown program in the High, or AboveNormal group, maybe a rogue process, or at least one worth investigating.

Real Life Mission: To group processes by their PriorityClass or Company name, incidentally, I cannot display this company information with Task Manager.

Purely PowerShell Missions: To introduce the pipeline | and to introduce group-Object

# PowerShell script to list processes grouped by Priority Class
# Author: Guy Thomas
# Version 1.2 August 2008 tested on PowerShell v 1.0

get-Process | group-Object -property PriorityClass

Learning Points

Note 1:  The pipeline (|) is a famous PowerShell technique for pumping the output of the first command (get-Process) into the second clause (group-Object).

Note 2:  The simplest form of this command is plain get-Process.

Note 3:  To apply what we learned in Example 1, and to count the processes try:

$Proc = get-Process
$Proc; "The number of processes is : " + $Proc.count

Example 2b: Group Process by Company

This cmdlet will list your processes grouped by their company name.

# PowerShell script to list processes grouped by Company
# Author: Guy Thomas
# Version 1.2 August 2008 tested on PowerShell v 1.0

get-Process | group Company

Note 1:  The verb ‘group’ is only ever associated with one noun called ‘Object’.  Thus you can omit the (-Object) part and PowerShell will deduce that there is only one meaning and so run the truncated command, group Company.

Note 2:  group-Object expects a property in position one, directly after the command, thus you can omit the -property parameter and PowerShell will ‘assume’ that company is a property.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary of Getting Started with Microsoft PowerShell

There are 5 techniques for getting self-help with Microsoft PowerShell.  Sometimes when I am trying to develop a script it does not produce the output that I want.  At this point I realize that I a different command could produce better results.  This week we have listed the available cmdlets with get-Command.  Next week we will investigate the operating system’s event logs.

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

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.