Start Process - PowerShell


Overview

Start-Process launches a new process in the system, allowing you to execute external programs, scripts, or commands. It’s commonly used for automation tasks, managing processes, and creating interactive applications.

Syntax

Start-Process [-FilePath] <string> [-ArgumentList] <string[]>
[-WorkingDirectory] <string> [-WindowStyle] <System.Diagnostics.ProcessWindowStyle>
[-Wait] [-PassThru] [-LoadUserProfile] [-Verb] <string> [-StandardInput] <string>
[-StandardOutput] <string> [-StandardError] <string>

Options/Flags

-FilePath: Specifies the path to the program or script to launch.

-ArgumentList: An array of strings representing command-line arguments to pass to the process.

-WorkingDirectory: Sets the initial working directory for the process.

-WindowStyle: Controls the appearance of the process window. Options include Normal, Hidden, Maximized, Minimized, Normal, and Hidden (see ProcessWindowStyle for details).

-Wait: Suspends the execution of the current script until the launched process completes.

-PassThru: Returns a Process object representing the launched process.

-LoadUserProfile: Loads the user profile associated with the current user in the new process.

-Verb: Specifies a verb to be used for the file path, such as Open, Edit, or Print.

-StandardInput: Sets the stdin stream for the process.

-StandardOutput: Sets the stdout stream for the process.

-StandardError: Sets the stderr stream for the process.

Examples

Launch a program:

Start-Process "notepad.exe"

Pass arguments to a program:

Start-Process "python.exe" -ArgumentList "script.py" "arg1"

Set the working directory:

Start-Process "cmd.exe" -WorkingDirectory "c:\temp"

Wait for a process to complete:

$p = Start-Process "ping.exe" -ArgumentList "google.com" -Wait
Write-Host $p.ExitCode

Common Issues

Missing file: Ensure the specified path to the program or script is correct.

Access denied: Check if you have sufficient permissions to execute the process.

Window appears briefly and exits: Try passing the -Wait flag to prevent the script from continuing until the process completes.

Integration

Get process information: Use the -PassThru flag to return a Process object, which you can query for various properties such as PID, status, memory usage, and exit code.

Chain commands: Combine Start-Process with other commands, such as Get-Process or Stop-Process, to manage processes programmatically.