Use array to check services' status

Hi guys,

Learning powershell and enjoying every minute of it.

At work, we have a morning checklist, and one of the items is for checking the status of some services on a server.

I can check the services status individually in a script 1 line at a time, but I want to use an array and pass it to the get-service command. This will definitely advance my powershell scripting knowledge.

What I have so far is 8 lines running the get-service command for each service and a pause command at the end so the console doesn’t close on me.

get-service -computername servername | where-object {$ -eq ‘ServiceName’}

So far in my research, I know that a variable is needed: $servicelist = @(‘service1’,‘service2’,‘service3’)

Also, probably the use of foreach. Just need to know how to put it all together.

Thank you.

“Give a man a fish and feed him for a day; teach a man to fish and you feed him for a lifetime.”


The Get-Service cmdlet takes a -Name parameter, so the filtering can happen at the cmdlet itself and not through the pipeline | where-object statement. It also takes multiple values for that parameter, so your entire command can go like:

get-service -computername servername -Name 'service1','service2','service3'

Sweeeeet! Thank you! And here I was complicating things… :smiley:

I’m going beyond the stated question, but since you mentioned learning Powershell. I thought you might be interested in exporting the results of the query to a file.

It will allow you to log the information over time and allow you to get more familiar with other aspects of Powershell. If so you might want to look at cmdlets such as Out-File or for a more data specific format Export-Csv. And for naming the file(s) consistently you can look at the Get-Date cmdlet and how to format it.