# Loop through servers and check services status

**URL:** <https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263>\
**Category:** PowerShell Help\
**Created:** [January 2, 2023, 8:04am UTC](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263 "2023-01-02T08:04:03Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 2, 2023, 9:42am UTC](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263/3 "2023-01-02T09:42:07Z")

</div>

aristosv,  
Welcome back to the forum. 👋🏽 … long time no see.

> [@aristosv](#):
>
> So I thought I would first define which services I want to check for each server.

Why do you use `if` statements for that? … and BTW:

> [@aristosv](#):
>
> `if ($servername = 'server1')`

In PowerShell the equal sign (`=`) is the assignment operator. If you want to check for equality you should use `-eq`. 👆🏽

> [@aristosv](#):
>
> I then realized I will have to create a function for each server I want to check.

Why do you think you need a function? … and BTW: `Set-Service` is used to change the settings of a given service - not to start or restart it. Therefor you should use `Start-Service` or `Restart-Service`.

> [@aristosv](#):
>
> How can I create a single function that will go through each server and check it’s corresponding services?

I’d recommend to save the needed information in a structured way - for example a CSV file. Thhen you can iterate with a nested loop of the servers and for each server over the services.

In general: what you’re asking for is a very common and simple task what has been asked a lot of times and already answered a lot of time. You don’t have to re-invent the wheel again. Use your prefered internet search engine to search for examples you can adapt to your particular needs. You will find some even here on [PowerShell.org](http://PowerShell.org).

Here you have some examples:

> [@Unable to get the Output of Hashtable in Table format](https://forums.powershell.org/t/unable-to-get-the-output-of-hashtable-in-table-format/21194/2):
>
> Anudeep, Welcome to the forum. wave:t4 I’d use a [PSCustomObject] instead of a hashtable in such a case. It’s even easier to read and maintain. param( [string[]]$computername ) foreach ($computer in $computername) { $filebeat\_status\_before = Get-Service -Name filebeat -ComputerName $computer Get-Service -Name filebeat -ComputerName $computer | Restart-Service $filebeat\_status\_after = Get-Service -Name filebeat -ComputerName $computer [PSCustomObject]@{ ComputerN…

> [@Get-Service with if/else checks](https://forums.powershell.org/t/get-service-with-if-else-checks/20535/2):
>
> Landon, Welcome to the forum. wave:t4 Since I like re-usable output I’d use a slightly different approach and avoid Write-Host prosa with fancy colors … wink Besides this I’d recommend to avoid connecting to one computer more than once at a time. $ComputerList = Get-Content -Path 'C:\\<file location\>\Desktop\Machines.txt' $ServiceQueryList = Get-Content -Path 'C:\\<file location\>\ServiceList.txt' $Result = foreach ($ComputerName in $ComputerList) { if (Test-Connection -ComputerName $C…

---

_[View the full topic](https://forums.powershell.org/t/loop-through-servers-and-check-services-status/21263)._
