PowerShell Get-Counter Cmdlet

 PowerShell Get-Counter

PowerShell's Get-Counter provides an alternative technique to employing Perfmon.  Normally it’s easy to be enthusiastic about PowerShell, but in the case of Get-Counter, I would not attempt to create scripts until I had a working knowledge of Performance Monitoring.

Topics for PowerShell's Get-Counter


Our Mission

Success or failure of Get-Counter missions depends on having clear objectives.  You need a working knowledge of Performance Monitoring because you need to make sense of strings such as:

"\Memory\Available Bytes"
"\Processor(_total)\% processor time"
"\LogicalDisk(_Total)\% Free Space"

Once you beg, borrow or copy the correct Performance counter, then preceding with Get-Counter is the easy part.  However, all is not plain sailing, you need to be able to interpret the data, and probably save to file (Out-File).

Example 1: Check Available Memory

The purpose of this script is to display the memory available on a machine called Win8, the units are Mbytes.  Incidentally, I cannot find a counter for Gigabytes.

# Employ PowerShell to measure computer data

Get-Counter -Counter "\Memory\Available MBytes"

Note 1:  The value for -Counter contains spaces, thus you need to enclose "\Memory\Available MBytes" in speech marks.

Typical result for Get-Counter

Timestamp                   CounterSamples
———                       ————–
06/06/2013 10:33:32   \\win8\memory\available mbytes :

Research More Performance Counters

This is how I discovered memory, processor and other computer performance counters.

The -ListSet parameter here reminds me of Get-Eventlog's -List, in this instance we can enumerate the processor performance counters.

Get-Counter -ListSet processor | Select-Object -ExpandProperty Counter

Note 1:  You really do need the full parameter name -ListSet.  PowerShell has lots of cmdlets that use just -List, but Get-Counter is NOT one of them.

Note 2:  Pure -ListSet * returns so many CounterSetNames that is why I filtered the command to display just the processor counters.  Also note that -ListSet on its own does not work, furthermore you need that space between -ListSet and the * wildcard.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 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 2: Real-life Task For Get-Counter

Enough doom and gloom.  If I have not put you off, then you must be keen to master Get-Counter, so here is a working example to monitor the processor.

# PowerShell Get-Counter Processor Example
Get-Counter -counter "\processor(_total)\% processor time" -continuous

Example 2a: Configure -SampleInterval Also, Save to File

# PowerShell Get-Counter Processor Example
Get-Counter -counter "\processor(_total)\% processor time" `
-SampleInterval 10 -MaxSamples 5 # | Out-File "C:\logs\processor.ps1"

Note 3:  You probably need to adjust C:\logs\processor.ps1 to a valid path on your computer.

Get-Help for Get-Counter

Thanks to parameters – some call them switches – you can modify the commands output to get the result you are looking for.  As usual, Get-Help is the key cmdlet.

# Research Get-Counter Parameters
Get-Help Get-Counter -Full

Note 4:  This is how I discovered ListSet.  PowerShell's examples show how you can tabulate just the names of the counters thus:

# PowerShell's Get-Counter List Processor Counters
(Get-Counter -ListSet Processor).Paths

Note 5: The key point is that .Paths is property of Get-Counter -ListSet Processor, a fact I discovered by piping the command into Get-Member.

Example 3: Monitor Process Virtual Bytes

While this is a real-life example to display memory usage of processes, it’s also meant to give you a template which you can alter to suit your project.  For example, you could change 'Virtual Bytes' for 'Working Set', or alternatively, you could choose a completely different counter, for example 'Memory' or 'Processor'.

$Proc = Get-Counter "\Process(*)\Virtual Bytes" -ErrorAction SilentlyContinue
$Proc.CounterSamples | Sort-Object CookedValue -Descending `
| Format-Table InstanceName, CookedValue -auto

Note 5: The key to this is example is 'CookedValue'.

InstanceName          CookedValue
————              ———–
_total           13650411520
powershell        949010432
iexplore           673783808
svchost           439242752
winlogon           59584512
dllhost              58359808
system               5033984
smss                  4120576
idle 0

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

Example 4: WMI Alternative to Get-Counter

This example maybe a step too far, or it may just alert you do a parallel technique, where you employ WmiObject instead of Get-Counter.

This, and similar scripts, will only return data if you are logged on as an administrator.

# PowerShell Performance and WMI
$Disk = Get-WmiObject -class Win32_PerfRawData_PerfDisk_LogicalDisk
$Disk | Get-Member

Note 5:  The above little script reveals over 50 disk properties.  The main features of these counters are Read or Write, Average or Percentage.

Research Similar PowerShell Counter Cmdlets

# PowerShell Counter Cmdlet Research
Get-Command -Noun Counter

The above script reveals the sister commands Import-Counter and Export-Counter. While it’s moderately easy to see what these cmdlets do, I have never seen anyone use them in real-life.   See more on PowerShell's Get-Counter

Summary of PowerShell Get-Counter Cmdlet

To be frank, as someone who is a minor expert in both PowerShell and performance monitoring I would encourage to seek other methods of achieving your goals before turning to Get-Counter.

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


See More Windows PowerShell Examples of Real-life Tasks

PowerShell Tutorials  • PowerShell Examples  • IpConfig  • Get-Counter  • PowerShell NetSh

Monitor Performance – PowerShell  • PowerShell temp   • PowerShell Delete Temporary files

PowerShell WOL (Wake-on-Lan)  • Services   • Change Computer Description Registry

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.