Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start-Process redirect output to $null

Tags:

powershell

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?

like image 569
edwin Avatar asked Mar 20 '18 02:03

edwin


People also ask

How do I redirect a null error in PowerShell?

To redirect the error stream to null, you would apply 2>$null to the cmdlet that's throwing the error.


1 Answers

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.

like image 150
mklement0 Avatar answered Oct 20 '22 21:10

mklement0