PS /home/sdwheeler> grep sdwheeler /etc/passwd
sdwheeler:x:1000:1000:,,,:/home/sdwheeler:/bin/bash
Passing arguments to native commands
Most shells include features for using variables, evaluating expressions, and handling strings. But
each shell does these things differently. In PowerShell, all parameters start with a hyphen (-
)
character. In cmd.exe
, most parameters use a slash (/
) character. Other command-line tools may
not have a special character for parameters.
Each shell has its own way of handling and evaluating strings on the command line. When running
native commands in PowerShell that expect strings to be quoted in a specific way, you may need
adjust how you pass those strings.
For more information, see the following articles:
about_Parsing
about_Quoting_Rules
PowerShell 7.2 introduced a new experimental feature PSnativeCommandArgumentPassing
that improved
native command handling. For more information, see PSnativeCommandArgumentPassing.
Handling output and errors
PowerShell also has several more output streams than other shells. The bash
and cmd.exe
shells
have stdout and stderr. PowerShell has six output streams. For more information, see
about_Redirection and about_Output_Streams.
In general, the output sent to stdout by a native command is sent to the Success stream in
PowerShell. Output sent to stderr by a native command is sent to the Error stream in
PowerShell.
When a native command has a non-zero exit code, $?
is set to $false
. If the exit code is zero,
$?
is set to $true
.
However, this changed in PowerShell 7.2. Error records redirected from native commands, like when
using redirection operators (2>&1
), aren't written to PowerShell's $Error
variable and the
preference variable $ErrorActionPreference
doesn't affect the redirected output.
Many native commands write to stderr as an alternative stream for additional information. This
behavior can cause confusion in PowerShell when looking through errors and the additional output
information can be lost if $ErrorActionPreference
is set to a state that mutes the output.
PowerShell 7.3 added a new experimental feature PSnativeCommandErrorActionPreference
that allows
you to control whether output to stderr
is treated as an error. For more information, see
PSnativeCommandErrorActionPreference.
Running PowerShell commands
As previously noted, PowerShell commands are known as cmdlets. Cmdlets are collected into PowerShell
modules that can be loaded on demand. Cmdlets can be written in any compiled .NET language or using
the PowerShell scripting language itself.
PowerShell commands that run other commands
The PowerShell call operator (&
) lets you run commands that are stored in variables and
represented by strings or script blocks. You can use this to run any native command or PowerShell
command. This is useful in a script when you need to dynamically construct the command-line
parameters for a native command. For more information, see the call operator.
The Start-Process
cmdlet can be used to run native commands, but should only be used when you need
to control how the command is executed. The cmdlet has parameters to support the following
scenarios:
Run a command using different credentials
Hide the console window created by the new process
Redirect stdin, stdout, and stderr streams
Use a different working directory for the command
The following example runs the native command sort.exe
with redirected input and output streams.
$processOptions = @{
FilePath = "sort.exe"
RedirectStandardInput = "TestSort.txt"
RedirectStandardOutput = "Sorted.txt"
RedirectStandardError = "SortError.txt"
UseNewEnvironment = $true
Start-Process @processOptions
For more information, see Start-Process.
On Windows, the Invoke-Item
cmdlet performs the default action for the specified item. For
example, it runs an executable file or opens a document file using the application associated with
the document file type. The default action depends on the type of item and is resolved by the
PowerShell provider that provides access to the item.
The following example opens the PowerShell source code repository in your default web browser.
Invoke-Item https://github.com/PowerShell/PowerShell
For more information, see Invoke-Item.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback.
Submit and view feedback for
This product