The PowerShell run command executes scripts, cmdlets, or external programs from within the PowerShell environment using the & operator or the Invoke-Expression cmdlet. It enables direct execution of code without launching a new session.
When you type a command in PowerShell, it parses and runs it in the current context. The & operator allows you to dynamically execute a string as a command. For example, & "Get-Process" runs the Get-Process cmdlet. This is essential for scripting changing behavior, such as building command strings based on user input or configuration files.
PowerShell uses a consistent syntax across all operations. The & symbol acts as a call operator, telling PowerShell to interpret the following string as executable code. Without it, PowerShell treats the string as a literal path or variable name. This prevents accidental execution of malformed inputs.
PowerShell evaluates expressions in a pipeline context. When you use & followed by a command string, PowerShell treats the entire string as a command to execute. The system checks the command's validity, resolves paths, and runs it in the current scope unless explicitly redirected.
For example, if you have a script file named "backup.ps1" in the current directory, running & .\backup.ps1 executes it. The dot (.) represents the current directory, and the backslash separates the path. This avoids needing to change directories or use full paths.
Using & is not limited to local files. You can run remote commands via PowerShell remoting. For instance, & { Get-Service } on a remote machine using Invoke-Command allows you to execute a block of code on a different system without manually logging in.
There are two primary ways to run commands in PowerShell: direct execution and changing invocation. Direct execution happens when you type a command directly into the console. Changing invocation uses & or Invoke-Expression to run strings as code.
| Type | Usage | Example | Use Case |
|---|---|---|---|
| Direct Command | Typing the command directly | Get-ChildItem | Quick file listing |
| Changing Run | Using & to execute a string | & "Get-Process | Where-Object {$_.CPU -gt 10}" | Script-generated commands |
| Remote Execution | With Invoke-Command | Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Service } | Administer multiple systems |
Changing execution is useful when constructing commands from variables. For example, if $cmd = "Get-NetIPConfiguration", then & $cmd runs the command stored in the variable. This pattern is common in configuration scripts and automated deployment tools.
PowerShell also supports passing arguments to scripts using the & operator. To run a script with parameters, use & .\script.ps1 -Param1 value1 -Param2 value2. This preserves argument binding and allows scripts to accept input during runtime.
Running untrusted commands via & or Invoke-Expression poses a security risk. Malicious code embedded in strings can execute arbitrary actions. PowerShell restricts execution by default through execution policies. The default setting, Restricted, blocks script execution entirely unless changed.
According to Microsoft documentation, execution policy settings control whether scripts can run. Set-ExecutionPolicy RemoteSigned allows local scripts to run but requires signed remote scripts. Always verify the source of any command string before executing it with &.
A common error occurs when using & with paths containing spaces. For example, & "C:\Program Files\MyApp\run.exe" fails if the path isn't quoted properly. Always wrap paths with spaces in double quotes to prevent parsing errors.
Another frequent mistake is assuming & runs in a new process. It does not. The command runs in the current session, which affects environment variables and state. If you need isolation, use Start-Process with the -FilePath parameter instead.
Suppose you're automating server setup and need to install a package using a tool called installer.exe located in C:\temp\installer.exe. You want to run it with specific flags: -silent -installpath C:\apps\myapp.
First, ensure the execution policy allows script execution:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Then, define the command as a string:
$command = 'C:\temp\installer.exe -silent -installpath "C:\apps\myapp"'
Finally, run it with &:
& $command
This executes the installer silently and installs the software to the specified directory. The quotes around the path prevent the space from breaking the argument list.
I've used this pattern in deployment scripts for Windows servers. A single line handles both the command construction and execution, reducing complexity and improving consistency across environments.
The PowerShell run command is not the same as the Start-Process cmdlet. While both launch processes, Start-Process creates a new process with independent memory and handles. The & operator runs code in the current session, sharing state and variables.
Also, & is not equivalent to the dot-source operator (.). The dot-source operator runs a script in the current scope, making functions and variables available afterward. Using & runs the script and returns control immediately, but doesn't import the namespace unless explicitly done.
Some users confuse & with the call operator in other languages like Bash. In PowerShell, & is unique to the shell and cannot be used as a function name or variable. It's a reserved operator with strict parsing rules.
Always validate input before passing it to & or Invoke-Expression. Never run user-provided strings directly. Use validation functions or allowlists to filter acceptable commands.
When writing scripts, prefer explicit command calls over changing execution. For example, use Get-Service instead of & "Get-Service". This improves readability and makes debugging easier.
If you need to run a command conditionally, wrap it in a function or use a switch statement. Avoid building complex command strings that are hard to audit or modify.
For administrators managing multiple machines, consider using PowerShell Remoting with Invoke-Command instead of manually running & on each host. This reduces errors and improves consistency.
When troubleshooting, enable verbose output with -Verbose or use the -Debug flag to see exactly what PowerShell is executing. This helps catch issues early, especially in automated workflows.
Understanding how the PowerShell run command works is essential for effective scripting and system administration. It's not just a way to run commands-it's a core mechanism for automation, configuration, and integration.