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

John Savill

April 14, 2008

1 Min Read
the word powershell on green coding background
Alamy

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:

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

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like