Problem
You want to access system performance counter information from PowerShell.
Solution
To retrieve information about a specific performance counter, use the System. Diagnostics.PerformanceCounter class from the .NET Framework, as shown in Example 156.
Example 156. Accessing performance counter data through the System.Diagnostics. PeformanceCounter class
PS >$arguments = "System","System Up Time" PS >$counter = NewObject System.Diagnostics.PerformanceCounter $arguments PS > PS >[void] $counter.NextValue() PS >NewObject TimeSpan 0,0,0,$counter.NextValue()
Days
: 0
Hours
: 18
Minutes
: 51
Seconds
: 17
Milliseconds
: 0
Ticks
: 678770000000
TotalDays
: 0.785613425925926
TotalHours
: 18.8547222222222
TotalMinutes
: 1131.28333333333
TotalSeconds
: 67877
TotalMilliseconds : 67877000
Alternatively, WMI’s Win32_Perf* set of classes support many of the most common performance counters:
GetWmiObject Win32_PerfFormattedData_Tcpip_NetworkInterface
Discussion
The System.Diagnostics.PerformanceCounter class from the .NET Framework provides access to the different performance counters you might want to access on a Windows system. Example 156 illustrates working with a performance counter from a specific category. In addition, the constructor for the PerformanceCounter class also lets you specify instance names, and even a machine name for the performance counter you want to retrieve.
The first time you access a performance counter, the NextValue() method returns 0. At that point, the system begins to sample the performance information and returns a current value the next time you call the NextValue() method.