powershellpowershell-core

pwsh Out-Null not suppressing the output on Restart-Service cmdlet


Using latest version of pwsh 7.5.1 x64 machine.

Looking to suppress the output from Restart-Service cmdlet by using Out-Null, but it's not doing its thing

Get-Service DusmSvc | Restart-Service | Out-Null

WARNING: Waiting for service 'Data Usage (DusmSvc)' to start...
WARNING: Waiting for service 'Data Usage (DusmSvc)' to start...

Any ideas, anyone?

enter image description here


Solution

  • Out-Null only suppresses output sent to the success stream, and what you're seeing are Warning messages. Use the -WarningAction common parameter to suppress this output:

    Get-Service DusmSvc | Restart-Service -WarningAction Ignore | Out-Null
    

    Alternatively, you could redirect the warning stream to success, then there shouldn't be any problems:

    Get-Service DusmSvc | Restart-Service 3>&1 | Out-Null
    

    Redirecting to $null would work too, in which case you mind as well remove the need for Out-Null and redirect all output streams:

    Get-Service DusmSvc | Restart-Service *>$null