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
- Example 1: Get-Process – The Basics
- Example 2: Get-Process – Check Companies
- Example 3: Group Process on Priority
- Example 4: Choosing Properties for Get-Process
- See Basic PowerShell Examples
♣
PowerShell Pre-requisites and Checklist
In the case of Windows 7 and later, you don’t need to download any extra files, just: ‘Add Feature’ –> Windows PowerShell. However, for older operating systems, 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 or later, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save 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: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance 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 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
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 • PowerShell Home • Windows PowerShell
• Top 10 Commands to Try in PowerShell • PowerShell v3 • Free CSV Import Tool
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.