Foreach If Service is Not Running Do . . .

by Aug 20, 2015


When every I get a chance I got back to the basic to properly learn PowerShell.

I created a simple script to get all services and if the status is stopped change the color to red if not stopped change it to green.

Not sure why but whether the service is stopped or running its all colored green.

What am I doing wrong?

 $AllServices = Get-Service
ForEach ( $SVC in $AllServices ) {
If ( $SVC.Status -eq "Stopped" ) {
Write-Host $SVC.Name, $SVC.status -ForegroundColor Red
Else {
Write-Host $SVC.Name, $SVC.status -ForegroundColor Green