PowerShell 2.0: One Cmdlet at a Time 3 Get-Counter

Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Get-Counter cmdlet.

What can I do with it?

Collect real-time performance counter data directly from local or remote computers.

Examples:

Create a list of performance counters available to query in the Memory counter

(Get-Counter -listset memory).paths

Tip: To find a list of available top-level counters for which you could substitute in for memory in the above example you could type this set of commands:

Get-Counter -listset * | Sort-Object countersetname | Format-Table countersetname

To retrieve the current Memory Pool Paged Bytes on the remote computer Server1

Get-Counter -counter ‘\Memory\Pool Paged Bytes’ -ComputerName Server1

Tip: You can run multiple samples using the -MaxSamples parameter

Get-Counter -counter ‘\Memory\Pool Paged Bytes’ -ComputerName Server1 -MaxSamples 5

How could I have done this in PowerShell 1.0?

You could use the Get-WMIObject cmdlet and the Win32_PerfFormattedData class to look at performance data for a remote computer. For example:

(Get-WmiObject Win32_PerfFormattedData_PerfOS_Memory -ComputerName Server1).PoolPagedBytes

You could also use .NET and the System.Diagnostics.PerformanceCounter class to view performance data

$data = New-Object System.Diagnostics.PerformanceCounter $data.CategoryName = “Memory” $data.CounterName = “Pool Paged Bytes” $data.nextvalue()

Thanks to /\/\o\/\/ for the .NET info.

1000 things 1% better!