Taking on PowerShell one cmdlet at a time

Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will show you how to use each PowerShell command each week. Adam will be covering Get-Counter this week.

When should you use Get-Counter
The Get-Counter cmdlet retrieves performance counter data directly from performance monitoring instruments in Windows operating systems. Get-Counter retrieves performance data from either a local or remote computer.
The Get-Counter parameters can be used to specify one or several computers, list the performance count sets and the instances within them, set the sample intervals and specify the maximum number. Get-Counter does not require parameters to retrieve performance counter data for a given set of system counters.
Access control lists (ACL) protect many counter sets. Open PowerShell and select the Run as administrator option to see all counter sets.
What version of PowerShell should I use for this blog?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.

How to use Get-Counter
Modules can be imported into the current session
Get-Counter uses a -ListSet parameter with an (*) to get a list of counter sets. The dot (.). The local computer is represented by the dot (.) in the MachineName column.

Please specify the SampleInterval & MaxSamples.
Get-Counter -Counter “\Processor(_Total)\% Processor Time” -SampleInterval 2 -MaxSamples 3
Get-Counter uses Counter to specify the counter path: Processor(_Total), % Processor Time.
The SampleInterval parameter specifies a two-second interval for checking the counter.
MaxSamples determined that the maximum number of times you can check the counter is three.

List alphabetical of counter sets:
Sort-Object -Property CounterSetName
Get-Counter uses a -ListSet parameter with an * (*), to obtain a complete list. The CounterSet objects are sent to the pipeline.
Sort-Object uses -Property to specify that objects are sorted using CounterSetName.
The objects are sent to Format-Table via the pipeline. The -AutoSize parameter adjusts column widths to minimize truncation.
The dot (..) in the MachineName column represents the local computer. The local computer is represented by the dot (.) in the MachineName column.

To get counter data, you can do a background job:
Start-Job -ScriptBlock Get-Counter -Counter “\LogicalDisk(_Total)\% Free Space” -MaxSamples 1000
Start-Job uses a -ScriptBlock parameter in order to run a GetCounter command.
Get-Counter uses the -Counter parameter to specify the counter path \LogicalDisk(_Total)\% Free Space.
The -MaxSamples parameter allows you to obtain 1000 samples of the counter.
** To see the performance counter output for the job, use Receive-Job cmdlet.

Multiple computers can provide counter data:
$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 goes down the pipeline to Get-Counter. Counter is the first position parameter. It accepts the $DiskReads path.
-ComputerName indicates the two computers, and -MaxSamples specifies how many samples each computer will produce.

Each counter in a counter-set should have a single value
$MemCounters = (Get-Counter -ListSet Memory).Paths
Get-Counter -Counter $MemCounters
Get-Counter uses -ListSet to specify the Memory counter set.
The command is enclosed within parentheses to ensure that the Paths property returns each path in string form.
The $MemCounters variable stores the paths. Get-Counter uses a parameter called -Counter to specify counter paths in the $MemCounters variables.

Get-InstalledModule – Last week’s command
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.