Search code examples
powershellsuppressmessage

Powershell Get-Service (Stop) but NOT display status "waiting for <x> service to stop..."


Basically, I don't have an issue with the action being taken. My question is about the status output of the action "waiting for..." message. Is there a way to suppress that message?

Example:

PS C:\Users\a.real.person.maybe> Get-Service *fewserviceswithmatchingprefixes* | Where-Object {$_.Name -notmatch 'somethinghere' -and $_.Name -ne 'alsosomethinghere' -and $_.Name -ne 'somethinghereprobably'} | Stop-Service
WARNING: Waiting for service 'thoseservicesabove' to stop...
WARNING: Waiting for service 'anotheroneofthoseservicesabove' to stop...

enter image description here


Solution

  • You can either set the value of the $WarningActionPreference variable at the callsite to Ignore or SilentlyContinue (both will suppress consumption and rendering of the warning message):

    $WarningActionPreference = 'SilentlyContinue'
    Get-Service *fewserviceswithmatchingprefixes* | Where-Object {$_.Name -notmatch 'somethinghere' -and $_.Name -ne 'alsosomethinghere' -and $_.Name -ne 'somethinghereprobably'} | Stop-Service
    

    Or you can use the -WarningAction common parameter explicitly when making the call to Stop-Service (it will only affect that particular invocation of Stop-Service):

    ... | Stop-Service -WarningAction SilentlyContinue