Get-Counter | Taking on PowerShell one cmdlet at a time | Weekly Blog

Share this post:

This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Counter.

When to use Get-Counter?

The Get-Counter cmdlet gets performance counter data directly from the performance monitoring instrumentation in the Windows family of operating systems. Get-Counter gets performance data from a local computer or a remote computer.

You can use the Get-Counter parameters to specify one or more computers, list the performance counter sets and the instances they contain, set the sample intervals, and specify the maximum number of samples. Without parameters, Get-Counter gets performance counter data for a set of system counters.

Many counter sets are protected by access control lists (ACL). To see all counter sets, open PowerShell with the Run as administrator option.

What version of PowerShell am I using for this blog?

Get the PowerShell Version from your machine:

$PSVersionTable

This command shows you the PowerShell version information on your machine.

How to use Get-Counter?

Get modules imported into the current session:

Get-Counter -ListSet * | FT

Get-Counter uses the –ListSet parameter with an asterisk (*) to get the list of counter sets. The dot (.) in the MachineName column represents the local computer.

Specify the SampleInterval and MaxSamples:

Get-Counter -Counter “\Processor(_Total)\% Processor Time” -SampleInterval 2 -MaxSamples 3

Get-Counter uses the Counter parameter to specify the counter path \Processor(_Total)\% Processor Time.

The SampleInterval parameter sets a two-second interval to check the counter.

MaxSamples determines that three is the maximum number of times to check the counter.

Alphabetical list of counter sets:

Get-Counter -ListSet * | Sort-Object -Property CounterSetName | Format-Table CounterSetName, CounterSetType -AutoSize

Get-Counter uses the –ListSet parameter with an asterisk (*) to get a complete list of counter sets. The CounterSet objects are sent down the pipeline.

Sort-Object uses the –Property parameter to specify that the objects are sorted by CounterSetName.

The objects are sent down the pipeline to Format-Table. The –AutoSize parameter adjusts the column widths to minimize truncation.

The dot (.) in the MachineName column represents the local computer.

Run a background job to get counter data:

Start-Job -ScriptBlock {Get-Counter -Counter “\LogicalDisk(_Total)\% Free Space” -MaxSamples 1000}

Start-Job uses the –ScriptBlock parameter to run a Get-Counter command.

Get-Counter uses the –Counter parameter to specify the counter path \LogicalDisk(_Total)\% Free Space.

The –MaxSamples parameter specifies to get 1000 samples of the counter.

** To view the performance counter output from the job, use the Receive-Job cmdlet.

Get counter data from multiple computers:

 $DiskReads = “\LogicalDisk(C:)\Disk Reads/sec”

$DiskReads | Get-Counter -ComputerName SCCMSSPRIME, SCCMDC -MaxSamples 10

The $DiskReads variable stores the \LogicalDisk(C:)\Disk Reads/sec counter path.

The $DiskReads variable is sent down the pipeline to Get-CounterCounter is the first position parameter and accepts the path stored in $DiskReads.

-ComputerName specifies the two computers and –MaxSamples specifies to get 10 samples from each computer.

Get a single value for each counter in a counter set:

$MemCounters = (Get-Counter -ListSet Memory).Paths

Get-Counter -Counter $MemCounters

Get-Counter uses the –ListSet parameter to specify the Memory counter set.

The command is enclosed in parentheses so that the Paths property returns each path as a string.

The paths are stored in the $MemCounters variable. Get-Counter uses the –Counter parameter to specify the counter paths in the $MemCounters variable.

Learn last week’s command: Get-InstalledModule.

Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.

Leave a Reply

Your email address will not be published. Required fields are marked *