Q. How I can I use Windows PowerShell to get a count of all of my machine's services?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 the word powershell on green coding background](https://eu-images.contentstack.com/v3/assets/blt07f68461ccd75245/blt3aa3a62c6e841c75/66e86a9c7daa46b3070b7c9f/the_word_powershell_on_green_coding_background.jpg?width=1280&auto=webp&quality=95&format=jpg&disable=upscale)
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