This page of
PowerShell examples concentrates on Windows processes such as you can see in
Task Manager. We will begin by learning how scripting can mimic what
you can do with taskmgr, then progress to displaying data that is just not
visible in the Task Manager GUI.
In the case of Windows 7 and Server
2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.
However, for older operating systems, installing can be confusing because
there are different versions of PowerShell for XP, Windows Server 2003 and
Vista. For such legacy systems only, you need to
download PowerShell from Microsoft's site.
Once you have installed PowerShell 2.0, I recommend choosing the
ISE (Integrated Scripting Engine) version, it
will save you buying a text editor.
# Simple PowerShell example to list Windows Processes
Get-Process
Note 1: One unsung method that has served me well in
understanding PowerShell's ways is to open the corresponding GUI and then
compare the script results with what I can see in the interface. This
technique works particularly well for issuing Get-Process commands and then
checking with Task Manager's Processes tab.
Note 2: The basic unit of PowerShell is the cmdlet.
This is a combination of keywords, where the first is a verb and the second
a noun.
One aspect of scripting is to automate routine tasks, another aspect is to
persuade PowerShell to do stuff that you can cannot do in the corresponding
GUI, in this instance, to list Processes grouped by company. When I
started using Task Manager I did not even realize there was a Company column
or property.
# PowerShell example to list processes by company Clear-Host Get-Process |
Group-Object Company | Sort-Object Count -Descending
Note 3: Pay close attention to the (|),
this pipe is secret to understanding how to get the most out of
PowerShell. The concept is to stream the output of Get-Process
into Group-Object.
Note 4: What I particularly like about this piping
of output into input is that you can build scripts in stages. Most of
my work on this site is to make sure you understand each basic stage.
Then it's up to you to bolt together multiple units to form a working
script, which solves your particular problem.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
Operating System processes have priorities, here is a quick script to
collate processes based on their priority level.
# PowerShell script to list processes grouped by Priority Class
Clear-Host Get-Process | Group-Object -property PriorityClass `
-errorAction SilentlyContinue
Note 4: From a pure PowerShell
point-of-view, observe the ` (Backtick) which says continue the
command on the next line, just like word-wrap.
Further Research for PowerShell in General and Get-Process in Particular
Before I get to Example 4 let us trace my thought processes to explain
how arrived at this final example.
Get-Help
If you check the parameters of any PowerShell cmdlets it will increase
your range of skills, and possibly solve a nagging problem. Thus
get into the habit of running Get-Help in front of any new cmdlet, thus:
# PowerShell Research Cmdlet Parameters Clear-Host Get-Help
Get-Process -full
Note 6: With Get-Help always append the -full
switch and thus see examples. One bonus of this research is
the realization, or reminder that we could use -computer to discover
what's happening with processes on another machine. We can
also see -errorAction listed amongst the CommonParameters.
Get-Member
While not all cmdlets benefit from Get-Member's ability to research
properties, Get-Process is a classic case where selecting the properties
in the output can dramatically improve your script.
Note 7: Two trivial points: unlike help, you
do need that pipe (|). Secondly remember the sequencing, in
this example it's cmdlet first, followed by the pipe and then
Get-Member.
Note 8: I have focussed this command by appending
the -memberType parameter and asking it to filter on properties
(and exclude methods).
Guy Recommends: Solarwinds' Free 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 to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
The purpose of these tutorials was two-fold, firstly, to encourage you
to employ PowerShell's basic learning techniques of Get-Help and
Get-Member. Secondly to illustrate how looking at the
corresponding GUI can not only check that a script is working, but
examining the menus and options will give you ideas for even better
scripts.
If you like this page then please share it with your friends
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.