Ezine 197 – Use PowerShell to Do Stuff You Cannot Do in a GUI

Ezine 197 – Use PowerShell to Do Stuff You Cannot Do in a GUI

The more reasons that you find to use PowerShell, the more likely you are to cope with the frustrations of learning something new, and come out the other side proficient at interrogating computers from the command-line.  This week we are going to employ PowerShell to do stuff you cannot do with a GUI.  We will also start using simple programming techniques to make the cmdlets even more useful.

Topics for Learning PowerShell v 2.0 Commands


This Week’s Secret

One of life’s mysteries is how do experts learn command-line languages.  My bet is that for most scripting gurus formal training plays no part.  While I had great satisfaction training Windows Server to techies, my DOS or VBScript classes never had the same feeling of success.  Either the training came too late, the delegates new the stuff already, or they knew very little and lacked the desire and aptitude for configuring a computer by typing commands.

My guess is that people learn scripting languages like DOS, VBScript and PowerShell, through copying other people’s code from the internet, then reading books on how to modify what they have copied, topped off with a large dose of trial and error.  My self-appointed mission is get people started.  By giving you a few quick lessons, I want you to absorb enough of the basics to avoid frustration, and reach a level of competence where you can pursue your own branch of intermediate or even advanced PowerShell.

This Week’s Mission – Do Stuff You Cannot Do with a GUI

Our task is to investigate the processes running on your computer.  I have deliberately chosen some tasks that you cannot do with the Task Manager GUI.  Incidentally, I bet that you will find at least one surprise in the list.  I am guessing that you will spot a suspicious process that needs further investigation.

This Week’s Mission is also to use get-Service as a vehicle to reinforce the basics. For example, reap the benefits of get-Member, research with get-Command and remember that if all else fails: get-Help cmdlet name.

As I constantly recommend, whenever you learn a new PowerShell cmdlet, try to open the corresponding GUI.  That way you can understand what the script is doing, also the menus in the GUI give you ideas for improving the PowerShell code.  In this instance open the Task Manager while you code with get-Process.


Before we do stuff that you cannot do with the GUI, let’s get acquainted with PowerShell’s equivalent of the Task Manager: get-Process


Another good reflex to develop is that whenever you see a new cmdlet, research its properties with get-Member

get-Process | get-Member

Note 1: Too much information?  Filter by appending with -MemberType *property

Note 2: If you don’t understand how to ‘append with -MemberType property’, then back to basics:
get-Help get-Process

Note 3: Remember where to use the pipe (|), e.g. before get-Member.  And where you DON’T need a pipe, get-Help.

Now for the stuff you cannot do with the GUI, let us group the processes by Company

get-Process | sort-Object company `
| format-Table company, Product, name -groupBy company -auto

Note 1: I admit to leaping ahead here and introducing three new techniques all at once.  Sort-Object is self-explanatory, while piping (|) the output into format-Table allows us to control which properties to display.  Finally -groupBy emphasises that some companies have multiple processes.

Note 2: Please take the time to check the results and see if I am correct when I guess that at least one of YOUR processes is suspicious.

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

The Joy of Applying Programming Techniques to PowerShell

Is it just me? Or do you too get a thrill from writing a few lines of code, then enclosing them in a loop and thus repeating an instruction a thousand times in a second?  When you have a payload that you want to deliver iteratively to hundreds of objects, it would take hours by hand, but with a looping command such as ‘foreach’, PowerShell does all that work in a flash.  Hence a double whammy, a job done much faster than you could do with a GUI, and whoosh of satisfaction in developing programming techniques within PowerShell.

Looping with Foreach

In this example we are going list all process with a company name, and filter out those with a null value.  Let us imagine that are task is to find the path to the installation files.  Now all scripting languages support variables, with PowerShell you just initialize them with a dollar sign, thus $TaskManager.  Incidentally, look out for the special variable $Null in the script below.

The syntax need by Foreach is: ($item in $collection) {Payload}

$TaskManager = get-Process | Sort-Object Company
foreach ($Item in $TaskManager)
if ($Item.Company -ne $Null)
"{0,-30} {1,-25} {2,-40}" -f
$Item.Company, $Item.Name, $Item.Path

Note 1: What this script does is get a stream of processes with $TaskManager.

Note 2: At the heart of this script is the foreach loop ($Item in $TaskManager).  Check the logic of the ‘If’ statement which filters out built-in processes which have a null value for their Company property.  The rest of this short script deals with formatting and specifying which properties to display. -f is simply a formatting command to control the column numbers and justification of the output.

Challenge 1: It would make my day if you substituted other properties for .Path.  How would do that?  Research with:
get-Process -MemberType *Property.

Challenge 2: It would REALLY make my day if you found more scenarios for variants of this script.  If you do, please send them in and I will publish them.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

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.

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


See more Microsoft PowerShell tutorials

Introduction  • Dreams  • 3 Key Commands  • Cmdlet scripts  • Real life tasks

Remote PowerShell  • -Online  • WinRm and WSMan  • Learn PowerShell

PowerShell Home  • Microsoft PowerShell

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.