Skip to content

Provide guidance as to when Start-Process is appropriate vs. direct / &-based invocation  #6239

@mklement0

Description

@mklement0

Related: #5152

Using Start-Process to invoke console (terminal) programs is (almost always) inappropriate, but, unfortunately, very common - instead, such programs should be invoked by direct invocation / via &, the call operator.

Proper guidance at the start of the Start-Process topic would go a long way to help clear up the confusion:


Note: Start-Process launches the new process asynchronously by default; add -Wait to wait for the newly created process to terminate.

  • DO NOT use Start-Process if you want to run a console (terminal-based) program synchronously, in the same window, with its standard streams connected to PowerShell's streams and the exit code reflected in $LASTEXITCODE - just invoke such a program directly / via &
    (e.g. whoami.exe or & whoami.exe rather than Start-Process whoami.exe).

    • Even if you use Start-Process -NoNewWindow -Wait, you won't be able to capture or redirect the program's output (you can only save stdout and stderr (separately) to files, as text, via -RedirectStandardOut and -RedirectStandardError). Additionally, the process' exit code will not be reflected in $LASTEXITCODE when you use Start-Process.

    • However, if your use case really calls for Start-Process (see below) and you need to obtain the process exit code, you can add -PassThru to the Start-Process call, which returns a process-information object (System.Diagnostics.Process) whose .ExitCode property can be examined after the newly launched process has exited, which you can ensure by also passing -Wait to Start-Process, or by calling .WaitForExit() on the object later, or by checking if .HasExited indicates $true.

  • [Only needed on Unix] DO use Start-Process to launch a GUI program asynchronously on Unix-like platforms (e.g., Start-Proces gedit).

    • Note: On Windows, GUI programs launch asynchronously even with direct invocation / &, so Start-Process Notepad and Notepad have the same effect.
  • [Only needed on Unix] DO use Start-Process to launch a detached process via the standard nohup utility, i.e. a process that will run invisibly, detached from the calling terminal, sending its output to a file.

    • Note: On Windows, you can achieve a similar effect by launching with -WindowStyle Hidden (albeit without automatic saving of output in a file).
  • [Windows-only] DO use Start-Process for starting console applications in a new window.

    • On Unix-like platforms, -NoNewWindow is invariably implied, and use of Start-Process for console programs there only makes sense if either (a) they neither prompt for input nor produce output or (b) -Wait is also used - but then direct invocation / & is the better choice - see The Start-Process topic contains incorrect and misleading information about use on Unix-like platforms #3013

    • [Windows-only] With -WindowStyle <style> you can additionally control the new process' window style (both for console windows and the windows of GUI applications, though they latter may not respect the setting), such as whether to start the window maximized, minimized, or even hidden (see next point).

  • [Windows-only] DO use Start-Process -WindowStyle Hidden, if you want to launch a process hidden.

  • [Windows-only] DO use Start-Process with -Verb RunAs in order to launch a process elevated (with administrative privileges, with triggers a UAC security prompt), invariably in a new window.

    • Caveat: -Verb RunAs cannot be combined with the -RedirectStandard* parameters, so if you want to capture the elevated process' output in files, you'll need to launch a shell process with a command line that uses that shell's redirection features from inside the elevated process, along the lines of Start-Process -Verb RunAs cmd.exe '/c "net session > out.txt"'
  • [Windows-only] DO use Start-Process with -Credential if you want to launch a process with a different user identity, invariably in a new window.

    • Caveat: This can not be combined with -Verb RunAs, so in order to run as a different user and with elevation, Start-Process calls must be nested, as demonstrated in this Stack Overflow answer.

From a cross-platform perspective, the short of it is:

  • On Unix-like platforms, Start-Process is useless except for two (unusual) scenarios: launching a GUI application asynchronously and launching a detached process via nohup

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Labels

area-managementArea - Microsoft.PowerShell.Management modulearea-native-cmdsArea - native command support

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions