How to Run a Command in PowerShell With Arguments
Run an executable with arguments by typing the executable followed by each argument as its own PowerShell token; use the call operator (&) when the executable path is stored in a variable or contains spaces. Use Start-Process only when you need process control such as a separate window, a working directory, or waiting for completion. This guide covers direct executable calls, PowerShell scripts, inline commands, quoting, and reliable ways to check the result.
What PowerShell Does With Commands and Arguments
PowerShell distinguishes between three common jobs:
- Running a native executable, such as
git.exe,docker,python, or an application you installed. - Running a saved PowerShell script ending in
.ps1. - Starting a new PowerShell process with
pwshorpowershell.exe.
A native command is normally run directly, much like it is in Command Prompt or Bash. Its command-line switches belong to that executable, not to PowerShell. For example, -Format is meaningful only if the target program documents it.
PowerShell parses spaces before it starts the executable. That is why paths and argument values containing spaces must be quoted.
How to Run an Executable With Arguments
Use direct invocation for an executable, -File for a saved script, and -CommandWithArgs for an inline pwsh command that needs positional values.
Choose the method that matches what you are launching.
| You Need To Do | Use | Why |
|---|---|---|
| Run an executable and see its output in the current terminal | Direct invocation | Simplest option; PowerShell waits and preserves normal console output. |
| Run an executable whose path has spaces | Call operator & |
Clearly tells PowerShell to execute the quoted path. |
| Start a program with a controlled working directory, window, or asynchronous behavior | Start-Process |
Gives process-management options. |
Run a .ps1 script in a new PowerShell process |
pwsh -File or powershell.exe -File |
Passes script parameters after the script path. |
| Run inline PowerShell code with positional arguments | pwsh -CommandWithArgs |
Makes following values available through $args. |
Step 1: Run the Executable Directly When You Can
- Type a command that PowerShell can find on your
PATH, followed by its arguments. - Prefix a program in the current folder with
.\. - Use
&before a quoted executable path.
For an executable in the current directory:
.\report.exe -Input .\report.csv -Format json
For an executable stored under a path with spaces:
& "C:\Program Files\Acme Tools\report.exe" -Input "C:\Work Files\report.csv" -Format json
The & operator is important here. Without it, PowerShell treats the quoted path as a string instead of a command to run.
Direct invocation is usually the best default because it waits for the program to finish, displays output in your terminal, and sets $LASTEXITCODE for native executables.
Step 2: Store Arguments in an Array When Building Them Dynamically
Avoid constructing one large command string when values come from variables. Build an array so each item remains one argument, even if a value contains spaces.
$inputFile = "C:\Work Files\report.csv"
$arguments = @(
"-Input"
$inputFile
"-Format"
"json"
)
& "C:\Program Files\Acme Tools\report.exe" @arguments
This is safer and easier to read than concatenating text such as a path, quotes, and switches into one string.
Do not use the pipeline character (|) to append arguments. A pipeline sends PowerShell objects from one command to another; it does not add command-line arguments to an executable that has already started.
Step 3: Use Start-Process When You Need Process Control
Use Start-Process when you need to choose a working directory, open a separate process window, launch a GUI program, or run something without immediately blocking the current script.
The -ArgumentList parameter accepts a string or an array of strings. The argument names and values must match the target executable’s own command-line documentation.
Start-Process -FilePath "C:\Program Files\Acme Tools\report.exe" `
-ArgumentList '-Input "C:\Work Files\report.csv" -Format json' `
-WorkingDirectory "C:\Work Files" `
-Wait
Use -Wait when the next command depends on the application finishing. Without it, Start-Process normally returns as soon as the new process starts.
To retrieve the exit code after waiting, add -PassThru and inspect the process object:
$process = Start-Process -FilePath "C:\Program Files\Acme Tools\report.exe" `
-ArgumentList '-Input "C:\Work Files\report.csv" -Format json' `
-Wait `
-PassThru
$process.ExitCode
Start-Process is useful for execution control, but it is not required merely because an executable has arguments. Microsoft’s Start-Process documentation recommends direct native-command invocation unless you need that extra control.
Step 4: Check That the Command Finished Successfully
A command that prints no output is not necessarily successful. Check its exit code, expected files, or returned data.
For a directly invoked executable, inspect $LASTEXITCODE immediately afterward:
& .\report.exe -Input .\report.csv -Format json
$LASTEXITCODE
By convention, an exit code of 0 means success. A nonzero code means the executable reported a failure; consult that program’s output and documentation for the meaning of its specific code.
If your script depends on an output file, test that it was created:
Test-Path .\report.json
Run a PowerShell Script With Parameters
A PowerShell script can declare named parameters with a param block. Put the block at the beginning of the .ps1 file.
param(
[Parameter(Mandatory)]
[string]$Domain,
[switch]$All
)
"Domain: $Domain"
"All: $All"
Run that script from an existing PowerShell session by placing arguments after the script path:
.\Get-Script.ps1 -Domain Central
To explicitly start a new PowerShell 7 session, use pwsh -File:
pwsh -NoProfile -File .\Get-Script.ps1 -Domain Central
To start Windows PowerShell 5.1 instead, use powershell.exe:
powershell.exe -NoProfile -File .\Get-Script.ps1 -Domain Central
Script switches are usually either present or omitted. When a switch must explicitly be false, use colon Boolean syntax:
pwsh -File .\Get-Script.ps1 -Domain Central -All:$false
On Windows, pwsh -File accepts .ps1 files only; on other platforms, pwsh attempts to run other file types. Do not use it as a general launcher for .exe, .cmd, or other file types.
Arguments are interpreted first by the shell you are currently using. If you launch pwsh from PowerShell, use PowerShell environment-variable syntax:
pwsh -File .\test.ps1 -TestParam $env:windir
If you launch it from Command Prompt, use Command Prompt syntax instead:
pwsh -File .\test.ps1 -TestParam %windir%
A batch file may run from a different working directory, so use its own script-directory expansion rather than assuming .\ points to the batch file’s folder:
pwsh -File %~dp0test.ps1 -TestParam %windir%
A normally completed script started with -File returns exit code 0. If a scheduled task, CI job, or calling program must detect a failure, have the script explicitly exit with a nonzero code when it encounters an unrecoverable condition.
if (-not (Test-Path $Domain)) {
exit 1
}
Pass Arguments to an Inline pwsh Command
Use -Command when you want a new PowerShell process to run inline code. When -Command receives a string, it must be the final PowerShell startup parameter because the rest of the text belongs to that command.
pwsh -NoProfile -Command 'Get-ChildItem -Path "C:\Program Files"'
For inline code that needs positional arguments, use -CommandWithArgs. Values after the command become entries in $args.
pwsh -CommandWithArgs '$args | % { "arg: $_" }' arg1 arg2
This prints each positional value supplied after the inline command. -CommandWithArgs was added as an experimental feature in PowerShell 7.4 and became mainstream in PowerShell 7.5-preview.5, so confirm that the installed pwsh version supports it before using it.
$PSVersionTable.PSVersion
Use -File for a saved script with named parameters and -CommandWithArgs for short inline commands that consume $args. This avoids trying to force script-style arguments onto a plain -Command string.
Handle Quotes, Variables, and Complex Arguments
Use double quotes when PowerShell must expand a variable inside a value. Use single quotes when the value should be literal.
$folder = "C:\Work Files"
& .\report.exe -Input "$folder\report.csv"
This passes the expanded path as one argument. A value such as C:\Work Files\report.csv must stay quoted because the embedded space would otherwise split it into two arguments.
For complex or nested quoting when starting PowerShell itself, use -EncodedArguments <Base64EncodedArguments>. The Base64 input must represent text encoded as UTF-16LE.
$argumentText = '-Domain "Central" -Label "He said ""go"""'
$encodedArguments = [Convert]::ToBase64String(
[System.Text.Encoding]::Unicode.GetBytes($argumentText)
)
Run your script with pwsh and pass the required arguments using a supported command-line option.
Use encoded arguments only when a launcher, build system, or nested shell makes ordinary quoting impractical. It makes commands harder for people to inspect and troubleshoot. It is different from -EncodedCommand, which encodes the entire PowerShell command rather than only its arguments.
Verify and Troubleshoot Argument Problems
Command Not Found
If PowerShell cannot find the executable, use its full path or check whether it is on PATH.
Get-Command report.exe
A program in the current folder still needs the .\ prefix:
.\report.exe -Help
Arguments Are Split at Spaces
Put the full argument value in quotes, or build an array and splat it during direct invocation.
& .\report.exe -Input "C:\Work Files\report.csv"
Start-Process Launches but Output Is Missing
Start-Process starts a separate process and does not send its console output through the current PowerShell pipeline by default. Run the executable directly if you need to see or capture terminal output.
& .\report.exe -Input .\report.csv -Format json
A Command Works Interactively but Fails in Automation
Start automation with a predictable environment. -NoProfile prevents profile scripts, aliases, custom functions, and module-loading behavior from changing the command’s result.
pwsh -NoProfile -File .\Get-Script.ps1 -Domain Central
Frequently Asked Questions
How Do I Run an EXE With Arguments in PowerShell?
Run it directly by placing the executable first and each argument after it. If the executable path contains spaces, use the call operator before the quoted path.
& "C:\Program Files\Acme Tools\report.exe" -Input "C:\Work Files\report.csv" -Format json
Use Start-Process -ArgumentList <string[]> only when you need process behavior such as -Wait, a chosen working directory, or a separate window.
How Do I Pass Piped Values to an EXE?
A PowerShell pipeline passes objects, not command-line arguments. To invoke an executable once per item, use ForEach-Object and pass each object property as an argument.
Get-ChildItem -Filter *.txt | ForEach-Object {
& .\convert.exe $_.FullName
}
Whether an executable can read directly from standard input depends on that executable’s own documentation.
How Do I Get Command-Line Arguments in a PowerShell Script?
Use a param block for named, typed script parameters. Use $args for unbound positional values, though named parameters are clearer and easier to validate.
param(
[string]$InputPath,
[switch]$Force
)
When the script’s param block declares [string]$Domain, pwsh -File .\Get-Script.ps1 -Domain Central binds Central to that $Domain parameter.
How Do I Write a PowerShell Function With Parameters?
Define parameters in a param block inside the function, then pass those values to the executable as separate arguments. Throw an error when the executable reports a nonzero exit code so callers can handle failure.
function Invoke-Report {
param(
[Parameter(Mandatory)]
[string]$InputPath,
[ValidateSet("json", "csv")]
[string]$Format = "json"
)
& .\report.exe -Input $InputPath -Format $Format
if ($LASTEXITCODE -ne 0) {
throw "report.exe failed with exit code $LASTEXITCODE"
}
}
Invoke-Report -InputPath "C:\Work Files\report.csv" -Format json
Can pwsh -File Run Any Type of File?
On Windows, pwsh -File accepts .ps1 files only. On other platforms, pwsh attempts to run other file types. Run executables directly with their arguments, or use Start-Process -FilePath when you need process-management options. Microsoft documents these startup forms in its pwsh command-line reference; next, choose the form that matches whether you are launching an executable, a saved script, or inline PowerShell code.
