Find Required Services with PowerShell

Here’s a little PowerShell tidbit to get the status of all the required services. That is, the services that other services depend upon. When using Get-Service, this is the RequiredServices property which will be a collection of service objects.

get-service | where {$_.status -eq "running"} | select -expand RequiredServices

I’m intentionally omitting and results so that you can try these command out for yourself. Next we need to filter out the duplicates. You might think this would work:

get-service | where {$_.status -eq "running"} | select -expand RequiredServices | select Name -unique | sort name

And it does, but the selection is case-sensitive and you’ll see that some names are a mix of cases. If you just want a list of names, then this will work:

get-service | where {$_.status -eq "running"} | select -expand RequiredServices | select DisplayName -unique | sort Displayname

But I want to also see the status of the required services so I can see if any are not running. I need to use the service name because some of the required services are kernel level and Get-Service won’t retrieve them by their displayname. So the challenge comes back to the case issue with the service name. The answer of course is to make them all the same case.

get-service | where {$_.status -eq "running"} | select -expand RequiredServices | foreach {$_.name.tolower()} | sort | get-unique | get-service

I turn each service name into lower case, sort because I like organized results, get the unique names and then pipe each name back to get-service. If I wanted to I could pipe this to Where-Object to only get stopped services or display other information for these required services.

This is a pretty cool example of using the PowerShell pipeline because I’m starting and ending with Get-Service and processing objects through the pipeline to meet my objective, without any scripting or text parsing.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati


About the Author

PowerShell.org Announcer

This is the official account for PowerShell.org and sponsor announcements.