user2469010
user2469010

Reputation: 31

powershell get-service -forgroundcolor

I am trying to change the color of my get-service output. It does not seem to allow me to use the write-host -forgroundcolor parameter after get-service. Is there any way around this?

I have tried the following configurations:

get-service write-host -foregroundcolor "green"
get-service -foregroundcolor "green"

I can get it to kind of work if I save get-service into a variable and place write-host in front like so:

$var = get-service

write-host $var -foregroundcolor "green"

The problem with this is the output is not formatted correct and becomes a single line.

Upvotes: 2

Views: 866

Answers (2)

Kamil Procyszyn
Kamil Procyszyn

Reputation: 71

Or you can:

Write-Host -ForegroundColor Green (Get-Service | out-string)

Upvotes: 0

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

Get-Service | Out-String [-Stream] | Write-Host -ForegroundColor Green

According to Out-String's help topic:

The Out-String cmdlet converts the objects that Windows PowerShell manages into an
array of strings. By default, Out-String accumulates the strings and returns them as
a single string, but you can use the stream parameter to direct Out-String to return 
one string at a time. This cmdlet lets you search and manipulate string output as 
you would in traditional shells when object manipulation is less convenient.

By using the -Stream argument, you don't wait for Get-Service to complete execution but it takes inputs as it gets them.

Upvotes: 1

Related Questions