keepcalculator

06 Numbers and Powers

PowerShell Run EXE: All the Ways to Launch Programs

Card No. 06·81432 · Updated · Standard formula, shown below

To run an executable (.exe) from PowerShell, you can simply type its full path or name if it’s in the current directory, press Enter, and the program launches. For more control—like waiting for the program to finish or passing arguments—use the Start-Process cmdlet. This article explains all the common methods, their differences, and how to handle tricky cases like spaces in paths or admin requirements.

Why Running an EXE in PowerShell Is Different

PowerShell is a command-line shell and scripting language built on .NET. Unlike Command Prompt (cmd.exe), PowerShell treats text differently. When you type a command, PowerShell first checks if it’s a built-in alias, a cmdlet, a function, or a script file. Only if none of those match does it look for an executable in the current directory or in the system’s PATH environment variable. This means that typing notepad.exe works, but typing a full path like C:\Program Files\MyApp\app.exe requires special handling because the path contains spaces. PowerShell interprets the space as a separator between the command and its arguments, so you must wrap the path in quotes or use the call operator.

Method 1: Direct Invocation (Typing the Path)

The simplest way is to type the full path to the executable and press Enter. If the path has no spaces, it works directly:

C:\Windows\System32\calc.exe

If the path contains spaces, you must use the call operator & to tell PowerShell to treat the quoted string as a command to execute:

& "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"

Alternatively, you can use the --% stop-parsing symbol, which tells PowerShell to pass everything after it to the program without interpreting it:

C:\Program Files\MyApp\app.exe --% /silent

This is useful when the arguments themselves contain special characters that PowerShell would otherwise try to interpret.

Method 2: Start-Process Cmdlet

The Start-Process cmdlet is the most flexible and recommended way to run an executable, especially in scripts. It allows you to control the window style, wait for the process to exit, pass arguments, and even run as a different user. Basic usage:

Start-Process -FilePath "C:\Program Files\MyApp\app.exe"

To pass arguments, use the -ArgumentList parameter. Note that arguments are passed as a single string or an array of strings, and spaces inside an argument must be handled carefully. For example:

Start-Process -FilePath "C:\Program Files\MyApp\app.exe" -ArgumentList "/config", "C:\My Config.ini"

If you need the script to wait until the program finishes before continuing, use the -Wait switch:

Start-Process -FilePath "C:\Program Files\MyApp\app.exe" -Wait

This is essential when the next step depends on the program completing, like running an installer before using the installed software.

Method 3: Using the Call Operator &

The call operator & is a PowerShell operator that executes a command stored in a string or a variable. It’s the cleanest way to run an executable with a path that contains spaces. You can also store the path in a variable and call it:

$exe = "C:\Program Files\MyApp\app.exe"
& $exe -arg1 -arg2

This is particularly useful in scripts where the path might change. The call operator also allows you to invoke commands stored in variables, which is a common pattern in advanced scripts.

Method 4: Using Invoke-Expression (Not Recommended)

You might see Invoke-Expression (alias iex) suggested, but it’s generally discouraged because it evaluates a string as PowerShell code, which can lead to security risks if the string contains user input. For running an executable, it’s unnecessary and can cause quoting headaches. Example of why to avoid it:

Invoke-Expression "C:\Program Files\MyApp\app.exe /silent"

If the path contains spaces, you’d need to add extra quotes, and any special characters in arguments could be misinterpreted. Stick to the call operator or Start-Process.

How to Choose the Right Method

Here’s a quick guide to decide which method fits your scenario:

  • Interactive use, no spaces in path: Just type the path or name.
  • Interactive use, spaces in path: Use & "path".
  • Scripting, need to wait for exit: Use Start-Process -Wait.
  • Scripting, need to pass complex arguments: Use Start-Process with an array of arguments.
  • Need to run as administrator: Use Start-Process -Verb RunAs.
  • Need to capture output: Use direct invocation (the call operator) and assign to a variable, or use Start-Process with redirection.

Common Problems and Solutions

Here are typical issues you might encounter and how to fix them:

  • “The term ‘app.exe’ is not recognized…” This means PowerShell can’t find the executable. Check if the file is in the current directory (PowerShell doesn’t run from the current directory by default unless you prefix with .\) or if it’s in a folder listed in the PATH environment variable. To run an exe in the current directory, use .\app.exe.
  • Path with spaces not working: Wrap the entire path in quotes and use the call operator: & "C:\Program Files\MyApp\app.exe".
  • Arguments with spaces being split: When using Start-Process, pass arguments as an array: -ArgumentList "arg1", "arg2 with spaces". For direct invocation, quote each argument that contains spaces.
  • Program runs but script continues before it finishes: Use Start-Process -Wait or, for direct invocation, use Start-Process -Wait because direct invocation does not wait by default. Actually, direct invocation does wait for console applications, but not for GUI applications. To force waiting, use Start-Process -Wait or pipe the output: & "app.exe" | Out-Null.
  • Access denied or permission errors: The executable might require administrator privileges. Run PowerShell as Administrator, or use Start-Process -Verb RunAs to trigger a UAC prompt.

Running EXEs as Administrator

If the program needs elevated privileges, you can use Start-Process with the -Verb RunAs parameter:

Start-Process -FilePath "C:\Program Files\MyApp\app.exe" -Verb RunAs

This will prompt the user for consent via UAC (User Account Control). If the script is already running as administrator, it will launch without a prompt. Note that -Verb RunAs cannot be combined with -Wait in older PowerShell versions; you may need to use Start-Process and then Wait-Process to wait for it to exit.

Capturing Output from an EXE

Sometimes you need to capture the output that the executable writes to the console. Direct invocation captures output as an array of strings:

$output = & "C:\Tools\myapp.exe" --version
$output

Each line becomes an element in the array. If you need to capture both stdout and stderr, you can use redirection:

$output = & "C:\Tools\myapp.exe" 2>&1

This merges error output with standard output. Alternatively, Start-Process can redirect output to files using the -RedirectStandardOutput and -RedirectStandardError parameters.

Frequently Asked Questions

How do I run an EXE from PowerShell with arguments?

Use the call operator: & "C:\Path\app.exe" -arg1 value1 -arg2. For more complex arguments, use Start-Process -FilePath "..." -ArgumentList @("-arg1", "value1").

Why does PowerShell say “not recognized” when I type the exe name?

PowerShell doesn’t search the current directory by default. You need to prefix with .\ to run an executable from the current folder, e.g., .\app.exe.

How do I wait for the exe to finish before continuing?

Use Start-Process -FilePath "..." -Wait. For direct invocation, GUI programs return immediately, so Start-Process -Wait is the reliable way.

Can I run an EXE as administrator from PowerShell?

Yes, use Start-Process -FilePath "..." -Verb RunAs. This will trigger a UAC prompt unless the session is already elevated.

How do I run a program with spaces in the path?

Wrap the full path in quotes and use the call operator: & "C:\Program Files\MyApp\app.exe". This works in both interactive and script contexts.

What is the difference between & and Start-Process?

The call operator & runs the executable in the current console and can capture output directly. Start-Process creates a separate process and offers more control over waiting, window style, credentials, and redirection, but capturing output requires file redirection.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →