[ThemesGuy/solarwinds/2011/vm-console_leftpsps.htm]

PowerShell Examples - Get-Process

Windows PowerShell Examples Featuring Get-Process

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.

Windows PowerShell Examples Windows Services

 ♣

PowerShell Pre-requisites and Checklist

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.

PowerShell Example 1: Get-Process - The Basics

# 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.

Example 2: Get-Process - Check Companies

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!Solarwinds Free WMI Monitor

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.

Download your free copy of WMI Monitor

Example 3: Group Process on Priority

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.

Note 5:  -errorAction SilentlyContinue suppresses 'Access Denied' messages

 

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.

# PowerShell Research Cmdlet Parameters
Clear-Host
Get-Process | Get-Member -memberType Property*

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 ToolFree Download of 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 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:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Example 4: Choosing Properties for Get-Process

Following the research from Get-Member, I have decided to select the following properties for our task of sorting process by their start time.

# PowerShell Example script to list processes sorted by start time.
Clear-Host
Get-Process | Sort-Object StartTime -errorAction SilentlyContinue `
| Format-Table Name, StartTime, HandleCount, WorkingSet -auto

Note 8:  Once again note how PowerShell pipes the output of one command so that it becomes the input for the next instruction.

Note 9: -auto just tightens up the columns.

Where Next?  More PowerShell Examples

Summary of PowerShell Examples for Processes

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

 


See more Windows PowerShell tutorials

PowerShell Tutorials  • PowerShell examples  • PowerShell service  • PowerShell Loops

Process example  • PowerShell services  • Get-Member   • Windows 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.

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.

 *


Custom Search

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

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.

Download your free copy of WMI Monitor

 

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

Please report a broken link, or an error.