Skip navigation

Q. What is the quickest way to get an object count in Windows PowerShell?

A. In my April 15, 2008, FAQ (" Q: How can I use Windows PowerShell to get a count of all of my machine's services?"), I created a service count using a Foreach loop, which increased a counter by 1 for each object returned from the Get-Service command, as the following code shows:

get-service | foreach \{$t=0\} \{$t +=1\} \{"Total services: $t"\}

A number of readers pointed out that I should've used the Measure-Object cmdlet with the following commands:

get-service | measure-object | select count

The output would be:


If I hadn't included the | select count command, the computer would have outputted other counts that don't apply to what I wanted, such as information about averages, sums, maximums, and minimums of the values passed.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.