8

I'm starting a new process like this:

$p = Start-Process -FilePath $pathToExe -ArgumentList $argumentList -NoNewWindow -PassThru -Wait

if ($p.ExitCode -ne 0)
{
    Write-Host = "Failed..."
    return
}

And my executable prints a lot to console. Is is possible to not show output from my exe?

I tried to add -RedirectStandardOutput $null flag but it didn't work because RedirectStandardOutput doesn't accept null. I also tried to add | Out-Null to the Start-Process function call - didn't work. Is it possible to hide output of the exe that I call in Start-Process?

edwin
  • 203
  • 1
  • 4
  • 9

2 Answers2

12

Using call operator & and | Out-Null is a more popular option, but it is possible to discard standard output from Start-Process.

Apparently, NUL in Windows seems to be a virtual path in any folder. -RedirectStandardOutput requires a non-empty path, so $null parameter is not accepted, but "NUL" is (or any path that ends with \NUL).

In this example the output is suppressed and the file is not created:

> Start-Process -Wait -NoNewWindow ping localhost -RedirectStandardOutput ".\NUL" ; Test-Path ".\NUL"
False
> Start-Process -Wait -NoNewWindow ping localhost -RedirectStandardOutput ".\stdout.txt" ; Test-Path ".\stdout.txt"
True

-RedirectStandardOutput "NUL" works too.

sastanin
  • 40,473
  • 13
  • 103
  • 130
9

You're invoking the executable synchronously (-Wait) and in the same window (-NoNewWindow).

You don't need Start-Process for this kind of execution at all - simply invoke the executable directly, using &, the call operator, which allows you to:

  • use standard redirection techniques to silence (or capture) output
  • and to check automatic variable $LASTEXITCODE for the exit code
& $pathToExe $argumentList *> $null
if ($LASTEXITCODE -ne 0) {
  Write-Warning "Failed..."
  return
}

If you still want to use Start-Process, see sastanin's helpful answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775