PowerShell 2.0: One Cmdlet at a Time 28 Export-Counter

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

What can I do with it?

Take performance objects generated from the Get-Counter or Import-Counter cmdlets and export them as log files. Note: this cmdlet requires Windows 7 or Windows Server 2008 R2 or later.

Examples:

Retrieve some memory performance data from the local machine and export it to the standard Performance Monitor output file BLG.

Get-Counter ‘\Memory\Pool Paged Bytes’ -MaxSamples 10 | Export-Counter -Path C:\Memory.blg

You can also output directly to two other format types, CSV and TSV.

Get-Counter ‘\Memory\Pool Paged Bytes’ -MaxSamples 10 | Export-Counter -Path C:\Memory.csv -FileFormat CSV

How could I have done this in PowerShell 1.0?

In the Get-Counter post I showed an example using .NET to retrive performance data, but it would only return one result at a time, so not a lot of point to extract to a log file.

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

You could run the final command multiple times and output to a text file, but still not a particularly nice solution.

for ($i=1; $i -le 10; $i++){$data.nextvalue() | Out-File test.txt -Append}

Alternatively from the Performance Monitor GUI you could create a Data Collector Set to save performance data into a BLG file. You could then use the Relog.exe tool to convert the BLG file into CSV or TSV.

1000 things 1% better!