10

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?

1
  • you can try $ErrorActionPreference= 'silentlycontinue' at the top of the script Commented Mar 20, 2018 at 3:39

2 Answers 2

18

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.

Sign up to request clarification or add additional context in comments.

Comments

10

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.