Running executables

suggest change

Console applications

Three ways to run a console application:

PS> console_app.exe
PS> & console_app.exe
PS> Start-Process console_app.exe

GUI applications

Ways to start a GUI application:

PS> gui_app.exe (1)
PS> & gui_app.exe (2)
PS> & gui_app.exe | Out-Null (3)
PS> Start-Process gui_app.exe (4)
PS> Start-Process gui_app.exe -Wait (5)

GUI applications launch in a separate process, and will immediately return control to the PowerShell host.

Sometimes you want to wait until the GUI application finishes. | Out-Null only discards output. It does not wait for a GUI process. Use Start-Process -Wait (5).

Exit codes

PS> $LastExitCode
PS> $?
PS> $Error[0]

$LastExitCode is the exit code of the last native application. $? is $true when that exit code is 0. $Error[0] is the most recent PowerShell error record, not the native exit code.

Console streams

PS> $ErrorActionPreference = "Continue" (1)
PS> & console_app.exe *>&1 | % { $_ } (2)
PS> & console_app.exe *>&1 | ? { $_ -is [System.Management.Automation.ErrorRecord] } (3)
PS> & console_app.exe *>&1 | ? { $_ -is [System.Management.Automation.WarningRecord] } (4)
PS> & console_app.exe *>&1 | ? { $_ -is [System.Management.Automation.VerboseRecord] } (5)
PS> & console_app.exe *>&1 (6)
PS> & console_app.exe 2>&1 (7)

Stream 2 contains System.Management.Automation.ErrorRecord objects.

Some applications, such as git.exe, write progress to stderr. Check $LastExitCode before treating that stream as a failure.

A native program writes to stdout and stderr. Redirected stderr arrives as an ErrorRecord. It does not produce PowerShell warning or verbose records. *>&1 merges all PowerShell streams. It is available in Windows PowerShell 3.0 and later. Stream 6 (information) is included in Windows PowerShell 5.0 and later. In Windows PowerShell 2.0, redirect a numbered stream, as in (7).

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents