why my script is not writing my condition

I have the following

@(“Running”, “Stopped”).ForEach({ if($result = Get-Service | where { $.Status -like $ }) { Write-Output -InputObject “$($result)” } })

why it not writing my statuses???

Wow … that’s a strange way to get services with the status stopped and running. Try it this way:

@("Running", "Stopped").ForEach( { $Status = $_; if ($result = Get-Service | where { $_.Status -like "$Status*" }) { Write-Output -InputObject "$($result)" } })

The moment you use the pipeline variable ($_) from your “outer” loop you already replaced it with your “inner” pipeline variable.

The “default” Powershell way would be something like this:

Get-Service |
    Where-Object {$_.Status -eq 'Running' -or $_.Status -eq 'Stopped'} |
        Select-Object -Property Name

… much easier to read I think. :wink:

What exactly are you trying to accomplish? There are numerous things wrong, but the main issue is that you are trying to use the $_ context incorrectly, see this example:

@("Running", "Stopped").ForEach{ 
    #$_ Context is in foreach
    $status = $_               #$_ Context is from the pipeline
    if($result = Get-Service | where { $_.Status -like $status }) { 
        Write-Output -InputObject "$($result)" 
    } 
}

Piping to foreach can get confusing, so it’s typically better to do an explicit for loop:

ForEach ($status in @("Running", "Stopped")) { 
    if($result = Get-Service | where { $_.Status -like $status }) { 
        Write-Output -InputObject "$($result)" 
    } 
}

If you want to group by status, try Group-Object

PS C:\Users\rasim> Get-Service | Group-Object -Property Status

Count Name                      Group                                                                                                                                                                                                                            
----- ----                      -----                                                                                                                                                                                                                            
  156 Stopped                   {AarSvc_96a65, AJRouter, ALG, AppIDSvc...}                                                                                                                                                                                       
  136 Running                   {AESMService, Appinfo, AudioEndpointBuilder, Audiosrv...}                                                                                                                                                                        

[quote quote=198446]Wow … that’s a strange way to get services with the status stopped and running. Try it this way:

<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 7.20125px; left: 45px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
@("Running", "Stopped").ForEach( { $Status = $_; if ($result = Get-Service | where { $_.Status -like "$Status*" }) { Write-Output -InputObject "$($result)" } })
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The moment you use the pipeline variable ($_) from your "outer" loop you already replaced it with your "inner" pipeline variable.

The “default” Powershell way would be something like this:

PowerShell
3 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 7.20125px; left: 45px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
Get-Service |
Where-Object {$_.Status -eq 'Running' -or $_.Status -eq 'Stopped'} |
Select-Object -Property Name
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
... much easier to read I think. 😉

[/quote]
Thank you

I code it that way so it could run in 1 line without using semicolon, so your first solution works like a charm.

And what’s the advantage of that?

Typically, there are only two states (Running\Stopped), so it can be as simple as this:

$results = Get-Service | Select Name