Prof. Powershell

You Can PerfCount on Me

The Get-Counter cmdlet gets better in PowerShell 2.0, with easier access to performance counter data.

You've always been able to access performance counter information in Windows PowerShell. In PowerShell 1.0, you had a few more hoops to jump through and a fair bit of scripting was involved in order to get that data. But in PowerShell 2.0, the Get-Counter cmdlet is easy to use and provides a wealth of system and performance information without much effort.

Go ahead and run the cmdlet without any parameters on a Windows 7 client:

PS C:\> get-counter

The cmdlet has a default counter set with commonly used information. Of course if you've ever worked with performance counters you know there are many more. Performance counters are organized into sets and you can use the cmdlet to list them:

PS C:\> get-counter -ListSet * | more

If you prefer just a list of names, this expression simplifies the output:

PS C:\> get-counter -ListSet * | select CounterSetName

What if you want to get some IPv4 counter information? The Get-Counter cmdlet will require the path to each counter, which you can get with an expression like this:

PS C:\> (get-counter -listset ipv4).counter

Once you know the path, you can specify it as a parameter:

PS C:\> get-counter –counter \IPv4\datagrams/sec

The cmdlet generally only takes a single counter value, but you can pipe paths into the cmdlet. If you want values for all of the IPv4 counters, here's how:

PS C:\> (get-counter -listset ipv4).counter | get-counter

Because you're working with objects, you can simplify this even further if you don't care about the timestamp and simply want the counter values:

PS C:\> ((get-counter -listset ipv4).counter | get-counter).readings

I don't have space to go through all of the cmdlet's functionality -- working with remote computers, sample rates and more -- so be sure to look at the cmdlet help and examples for more information.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular