Collecting Performance Counters with PowerShell
See how you can access performance data using PowerShell
August 26, 2015
Q: Can I use PowerShell to collect system performance data without using Perfmon?
A: Yes. Using the PowerShell Get-Counter cmdlet you can directly query all of the available performance monitor counters that are in the system. For instance, the following line illustrates how to use the Get-Counter cmdlet to return the value of the Processor(_Total)% Processor Time counter which shows the total CPU usage for the system.
Get-Counter -counter "Processor(_Total)% Processor Time”
If you’re collecting data for analysis it’s important to realize that the Get-Counter cmdlet retrieves values for the point in time that it is run.
If you want to retrieve multiple values over a given interval you can add the -SampleInterval and –MaxSamples parameters to the Get-Counter cmdlet. The following sample shows how you can retrieve the Processor(_Total)% Processor Time value every 10 seconds until 60 values have been collected (a period of 10 minutes).
Get-Counter -counter "Processor(_Total)% Processor Time” –SampleInterval 10 –MaxSamples 60
You might note that the Get-Counter cmdlet retrieves a number of different pieces of information including the Timestamp and the CounterSample (essentially the counter name). To retrieve just the values of a given counter using the Get-Counter you need to retrieve the CookedValue item from the CounterSamples collection as you can see in the following examples.
$CPUTime = (Get-Counter -counter "Processor(_Total)% Processor Time”).CounterSamples.CookedValue
$AvailableMBytes = (Get-Counter -counter "MemoryAvailable MBytes”).CounterSamples.CookedValue
Here the values of the Processor(_Total)% Processor Time and MemoryAvailable MBytes counters are assigned to the variables $CPUTime and $AvailableMBytes respectively.
About the Author
You May Also Like