Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Better Performance Counters with PowerShell

Posted on November 12, 2020November 12, 2020

I wanted to tell you about another addition to the latest release of the PSScriptTools module. This is something I've written about before but I decided to add the function to the module. I hope you find it a much easier way to work with performance counters. And it works in Windows PowerShell and PowerShell 7.x.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

Get-Counter

We've had the Get-Counter cmdlet in PowerShell for a very long time. However, I have always had a slight problem with the way it displays results.

The command writes an interesting object to the pipeline.

Under the hood, PowerShell is using a custom format file to render results in an easy to read view. But if you want to do something with the output like export to a csv file, you need to jump through hoops.

Get-Counter | Select-Object -expand Countersamples | Select-Object Timestamp,Path,InstanceName,CookedValue,@{Name="Computername";Expression={$_.path.split("\")[2].toUpper()}} | Export-Csv c:\work\counters.csv

This is certainly not intuitive. I wanted a performance counter cmdlet that wrote "better" structured data to the pipeline.

Get-MyCounter

The PSScriptTools module now includes an updated version of my Get-MyCounter function. This function is a "wrapper" around Get-Counter that writes simple, structured objects to the pipeline. You can use it in almost the same say as Get=Counter.

The custom object has its own formatting file. But the object type is easy to use.

Now, I have a much easier way to work with performance counter data.

In fact, I can get counters from Get-Counter and pipe them to Get-MyCounter.

Get-MyCounter also supports sampling.

get-counter -list system | get-mycounter -MaxSamples 10 -SampleInterval 2

Or you could use ConvertTo-WPFGrid, also from the PSScriptTools module, and let it handle the sampling.

$counters = (get-counter -list system).counter | where {$_ -match "file"}
Get-MyCounter -Counter $counters -ComputerName prospero,thinkp1 | 
Select-Object * -exclude Instance | 
ConvertTo-WPFGrid -Title "System Performance" -Timeout 6 -refresh -UseLocalVariable counters

The form will refresh every 6 seconds until I close it. The form also runs in its own runspace so there is no prompt blocking.

I hope you'll give Get-MyCounter a try and let me know what you think.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “Better Performance Counters with PowerShell”

  1. Pingback: ICYMI: PowerShell Week of 13-November-2020 | PowerShell.org
  2. Pingback: ICYMI: PowerShell Week of 13-November-2020 – 247 TECH
  3. Pingback: Improving Performance Counters with Powershell – Curated SQL

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d