Q. How I can I use Windows PowerShell to get a count of all of my machine's services?

A. The Get-Service cmdlet returns information about all services. You can easily pass this output to a loop that increments a counter. The following code provides an example:

Users\john.SAVILLTECH> Get-service | foreach \{$t=0\} \{$t +=1\} \{"Total services: $t"\}

The return is:

Total services: 144

How does this work? The Foreach cmdlet has three sections. The first runs the \{$t=0\} variable once to set it to zero. The second section, \{$t +=1\}, increments the variable by one for each service, and the final section, \{"Total services: $t"\}, displays the variable's value when the cmdlet finishes.

Hide comments

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.
Publish