Gets performance counter data from local and remote computers.

Syntax

Get-Counter [-Counter] <string[]> [-ComputerName <string[]>] [-Continuous] [-MaxSamples <Int64>] [-SampleInterval <int>] [<CommonParameters>]

Get-Counter -ListSet <string[]> [-ComputerName <string[]>] [<CommonParameters>]

Description

The Get-Counter cmdlet gets live, real-time performance counter data directly from the performance monitoring instrumentation in Windows. You can use it to get performance data from the local or remote computers at the sample interval that you specify.

Without parameters, a "Get-Counter" command gets counter data for a set of system counters.

You can use the parameters of Get-Counter to specify one or more computers, to list the performance counter sets and the counters that they contain, and to set the sample size and interval.

Parameters

-ComputerName <string[]>

Gets data from the specified computers. Type the NetBIOS name, an Internet Protocol (IP) address, or the fully qualified domain names of the computers. The default value is the local computer.

Note: Get-Counter does not rely on Windows PowerShell remoting. You can use the ComputerName parameter of Get-Counter even if your computer is not configured for remoting in Windows PowerShell.

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Continuous

Gets samples continuously until you press CTRL+C. By default, Get-Counter gets only one counter sample. You can use the SampleInterval parameter to set the interval for continuous sampling.

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Counter <string[]>

Gets data from the specified performance counters. Enter one or more counter paths. Wildcards are permitted only in the Instance value. You can also pipe counter path strings to Get-Counter.

Each counter path has the following format:

"[\\<ComputerName>]\<CounterSet>(<Instance>)\<CounterName>"

For example:

"\\Server01\Processor(2)\% User Time".

The <ComputerName> element is optional. If you omit it, Get-Counter uses the value of the ComputerName parameter.

Note: To get correctly formatted counter paths, use the ListSet parameter to get a performance counter set. The Paths and PathsWithInstances properties of each performance counter set contain the individual counter paths formatted as a string. You can save the counter path strings in a variable or pipe the string directly to another Get-Counter command. For a demonstration, see the examples.

Required?

true

Position?

2

Default Value

none

Accept Pipeline Input?

true (ByValue)

Accept Wildcard Characters?

true

-ListSet <string[]>

Gets the specified performance counter sets on the computers. Enter the names of the counter sets. Wildcards are permitted. You can also pipe counter set names to Get-Counter.

Required?

true

Position?

named

Default Value

none

Accept Pipeline Input?

true (ByValue, ByPropertyName)

Accept Wildcard Characters?

true

-MaxSamples <Int64>

Specifies the number of samples to get from each counter. The default is 1 sample. To get samples continuously (no maximum sample size), use the Continuous parameter.

To collect a very large data set, consider running a Get-Counter command as a Windows PowerShell background job. For more information, see about_Jobs and Start-Job.

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-SampleInterval <int>

Specifies the time between samples in seconds. The minimum value and the default value are 1 second.

Required?

false

Position?

named

Default Value

1

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see about_CommonParameters.

Inputs and Outputs

The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet returns.

Inputs

System.String[]. You can pipe counter paths and counter set (ListSet) names to Get-Counter.

System.String[]. You can pipe counter paths and counter set (ListSet) names to Get-Counter.

Outputs

Microsoft.PowerShell.Commands.GetCounter.CounterSet, Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet, Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample

The ListSet parameter gets Microsoft.PowerShell.Commands.GetCounter.CounterSet objects. The Counter parameter gets Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet objects. Each counter value is a Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample object.

Notes

Performance counters are often protected by access control lists (ACLs). To get all available performance counters, open Windows PowerShell with the "Run as administrator" option.

By default, Get-Counter gets one sample during a one-second sample interval. To change this behavior, use the MaxSamples and Continuous parameters.

The MaxSamples and SampleInterval values that you set apply to all the counters on all the computers in the command. To set different values for different counters, enter separate Get-Counter commands for each counter.

Example 1

C:\PS>get-counter -ListSet *

This command gets all of the counter sets on the local computer.

Because many of the counter sets are protected by access control lists (ACLs), to see all counter sets, open Windows PowerShell with the "Run as administrator" option before using the Get-Counter command.






Example 2

C:\PS>get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 2 -MaxSamples 3

This command gets the current "% Processor Time" combined values for all processors on the local computer. It collects data every 2 seconds until it has three values.






Example 3

C:\PS>get-counter -listset * | sort-object  countersetname | format-table countersetname

This command gets an alphabetically sorted list of the names of all of the counter sets on the local computer.






Example 4

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

C:\PS> (get-counter -listset memory).paths | where {$_ -like "*cache*"}

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

\Memory\Page Faults/sec
\Memory\Available Bytes
\Memory\Committed Bytes
\Memory\Commit Limit
\Memory\Write Copies/sec
\Memory\Transition Faults/sec
\Memory\Cache Faults/sec
\Memory\Demand Zero Faults/sec
\Memory\Pages/sec
\Memory\Pages Input/sec
...

C:\PS> (get-counter -listset memory).paths | where {$_ -like "*cache*"}

\Memory\Cache Faults/sec
\Memory\Cache Bytes
\Memory\Cache Bytes Peak
\Memory\System Cache Resident Bytes
\Memory\Standby Cache Reserve Bytes
\Memory\Standby Cache Normal Priority Bytes
\Memory\Standby Cache Core Bytes


Description
-----------
These commands use the Path property of a counter set to find the correctly formatted path names for the performance counters. You can use a command like this one to get the correct counter path names.

The first command gets the path names of the performance counters in the Memory counter set on the local computer.  

The second command gets the path names that include "cache".






Example 5

C:\PS>$diskreads = "\LogicalDisk(C:)\Disk Reads/sec"

C:\PS> $diskreads | get-counter -computer Server01, Server02 -maxsamples 10

These commands get the Disk Reads/sec counter data from the Server01 and Server02 computers. The second command uses the MaxSamples parameter to limit the return to 10 samples.






Example 6

C:\PS>(get-counter -list physicaldisk).pathswithinstances

This command gets the correctly formatted path names for the PhysicalDisk  performance counters, including the instance names.






Example 7

C:\PS>C:\PS> $servers = get-random (get-content servers.txt) -count 50

C:\PS> $counter = "\Processor(*)\% DPC Time"

C:\PS> get-counter -Counter $counter -computername $servers

These commands get the value of the "% DPC Time" performance counter on 50 randomly select computers in the enterprise. 

The first command uses the Get-Content cmdlet to get the list of enterprise servers from the Servers.txt file. It uses the Get-Random cmdlet to select 50 server names randomly from the Servers.txt file contents. The results are saved in the $servers variable.

The second command saves the counter path to the "% DPC Time" cmdlet in the $Counter variable. The counter path includes a wildcard character in the instance name to get the data on all of the processors on each of the computers. 

The third command uses the Get-Counter cmdlet to get the counter values. It uses the Counter parameter to specify the counters and the ComputerName parameter to specify the computers saved in the $servers variable.






Example 8

C:\PS>$memCounters = (get-counter -list memory).paths

C:\PS> get-counter -counter $memCounters

These commands get a single value for all of the performance counters in the memory counter set on the local computer.

The first command gets the counter paths and saves them in the $memCounters variable.

The second command uses the Get-Counter cmdlet to get the counter data for each counter. It uses the Counter parameter to specify the counters in $memCounters.






Example 9

C:\PS>$counter = "\\SERVER01\Process(Idle)\% Processor Time" 

C:\PS> $data = get-counter $counter

C:\PS> $data.countersamples | format-list -property *

Path             : \\SERVER01\process(idle)\% processor time
InstanceName     : idle
CookedValue      : 198.467899571389
RawValue         : 14329160321003
SecondValue      : 128606459528326201
MultipleCount    : 1
CounterType      : Timer100Ns
Timestamp        : 7/15/2008 6:39:12 PM
Timestamp100NSec : 128606207528320000
Status           : 0
DefaultScale     : 0
TimeBase         : 10000000


Description
-----------
This example shows the property values in the PerformanceCounterSample object that represents each data sample.

The first command saves a counter path in the $counter variable. 

The second command uses the Get-Counter cmdlet to get one sample of the counter values. It saves the results in the $data variable.

The third command uses the Format-List cmdlet to display all the properties of the CounterSamples property of the sample set object as a list.

You can use the properties of the CounterSamples object to examine, select, sort,  and group the data.






Example 10

C:\PS>$counters = "\LogicalDisk(_Total)\% Free Space"

C:\PS> start-job -scriptblock {get-counter -counter $counters -maxsamples 1000)

The command runs a Get-Counter command as background job. For more information, see Start-Job.






Example 11

C:\PS>get-counter -computername (get-random servers.txt -count 50) -counter "\LogicalDisk(*)\% Free Space"

This command uses the Get-Counter and Get-Random cmdlets to find the percentage of free disk space on 50 computers selected randomly from the Servers.txt file.






Example 12

C:\PS>$a = get-counter "\LogicalDisk(_Total)\% Free Space" -comp s1, s2

C:\PS> $a

C:\PS> $a.countersamples | format-table -auto

C:\PS> $a.countersamples[0] | format-table -property * 

C:\PS> $a.countersamples | where {$_.cookedvalue -lt 15}

C:\PS> $a

Counter Paths: \\s1\\logicaldisk(c:)\% free space, \\s1\\logicaldisk(d:)\% free space, \\s1\\logicaldisk(_total)\% free space, \\s2\\logicaldisk(c:)\% free space, \\s2\\logicaldisk(_total)\% free space

Timestamp     : 7/15/2008 5:09:08 PM
Cooked Values : "0.327058823529412", "17.8952248493278", "12.9994033060778", "75.0754805595626", "75.0754805595626"

C:\PS> $a.countersamples | format-table -auto

Path                                     InstanceName       CookedValue
----                                     ------------       -----------
\\s1\\logicaldisk(c:)\% free space       c:           0.327058823529412
\\s1\\logicaldisk(d:)\% free space       d:            17.8952248493278
\\s1\\logicaldisk(_total)\% free space   _total        12.9994033060778
\\s2\\logicaldisk(c:)\% free space       c:            75.0754805595626
\\s2\\logicaldisk(_total)\% free space   _total        75.0754805595626


C:\PS> ($a.countersamples)[0] | fl *


Path             : \\s1\\logicaldisk(c:)\% free space
InstanceName     : c:
CookedValue      : 0.327058823529412
RawValue         : 139
SecondValue      : 42500
MultipleCount    : 1
CounterType      : RawFraction
Timestamp        : 7/15/2008 5:09:08 PM
Timestamp100NSec : 128606153488500000
Status           : 0
DefaultScale     : 0
TimeBase         : 3312060


C:\PS> $a.countersamples | where {$_.cookedvalue -lt 15}

Path                                     InstanceName       CookedValue
----                                     ------------       -----------
\\s1\\logicaldisk(c:)\% free space       c:           0.327058823529412
\\s1\\logicaldisk(_total)\% free space   _total        12.9994033060778


Description
-----------
This example shows how to associate counter data with the computer on which it originated, and how to manipulate the data.

The first command uses the Get-Counter cmdlet to get the "LogicalDisk\% Free Space" counter value from two remote computers, S1 and S2. It saves the result in the $a variable.

The second command displays the results in the $a variable. All of the data is stored in the object, but it is not easy to see it in this form.

The third command displays in a table the value of the CounterSamples property of the PerformanceCounterSampleSet object that Get-Counter returns. (To see all of the properties and methods of the object, pipe it to the Get-Member cmdlet.) 

The CounterSamples property contains a PerformanceCounterSample object with its own properties and methods. The fourth command uses array notation to get the first counter sample and a pipeline operator to send the counter sample object to the Format-List cmdlet, which displays all of its properties and methods in a list. This display shows the richness of the data in each counter sample object.

The fourth command shows how to select data from the counter samples. It uses the Where-Object cmdlet to get only the counter samples with a CookedValue of less than 15.






Example 13

C:\PS>$p = get-counter '\Process(*)\% Processor Time' 

C:\PS> $p.CounterSamples | sort-object -property CookedValue -Descending | format-table -auto

Path                                              InstanceName      CookedValue
----                                              ------------      -----------
\\server01\process(_total)\% processor time        _total        200.00641042078
\\server01\process(idle)\% processor time          idle          200.00641042078
\\server01\process(explorer#1)\% processor time    explorer                    0
\\server01\process(dwm#1)\% processor time         dwm                         0
\\server01\process(taskeng#1)\% processor time     taskeng                     0
\\server01\process(taskhost#1)\% processor time    taskhost                    0
\\server01\process(winlogon)\% processor time      winlogon                    0
\\server01\process(csrss)\% processor time         csrss                       0


Description
-----------
This example shows how to sort the performance counter data that you retrieve. The example finds the processes on the computer that are using the most processor time during the sampling.

The first command gets the "Process\% Processor Time" counter for all the processes on the computer. The command saves the results in the $p variable.

The second command gets the CounterSamples property of the sample set object in $p and it sorts the samples in descending order based on the cooked value of the sample. The command uses the Format-Table cmdlet and its AutoFormat parameter to position the columns in the table.






Example 14

C:\PS>$ws = get-counter "\Process(*)\Working Set - Private"

C:\PS> $ws.countersamples | sort-object -property cookedvalue -descending | format-table -property InstanceName, CookedValue -auto

InstanceName  CookedValue
------------  -----------
_total          162983936
svchost          40370176
powershell       15110144
explorer         14135296
svchost          10928128
svchost           9027584
...


Description
-----------
These commands find the processes on the computer with the largest working sets. They list the processes in descending order based on their working set size. 

The first command gets one sample of the "Process\Working Set - Private" counter for each process. The command saves the counter data in the $ws variable.

The second command uses a pipeline operator (|) to send the data in the CounterSamples property of the $ws variable to the Sort-Object cmdlet, where the process data is sorted in descending order by the value of the CookedValue property. Another pipeline sends the sorted data to the Format-Table cmdlet, where the data is formatted as a table with InstanceName and CookedValue columns.






Example 15

C:\PS>get-counter -counter "\processor(_total)\% processor time" -continuous

This command gets a series of samples of the Processor\% Processor Time counter at the default one second interval. To stop the command, press CTRL + C.






See Also




Table Of Contents