Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
PowerShell is a command-line shell and a scripting language used for automation. Similar to other
shells, like bash on Linux or the Windows Command Shell (cmd.exe), PowerShell lets you run any
command available on your system, not just PowerShell commands.
Types of commands
For any shell in any operating system there are three types of commands:
Shell language keywords are part of the shell's scripting language.
- Examples of
bashkeywords include:if,then,else,elif, andfi. - Examples of
cmd.exekeywords include:dir,copy,move,if, andecho. - Examples of PowerShell keywords include:
for,foreach,try,catch, andtrap.
Shell language keywords can only be used within the runtime environment of the shell. There's no executable file, external to the shell, that provides the keyword's functionality.
- Examples of
OS-native commands are executable files installed in the operating system. The executables can be run from any command-line shell, like PowerShell. This includes script files that require other shells to work properly. For example, if you run a Windows batch script (
.cmdfile) in PowerShell, PowerShell runscmd.exeand passes in the batch file for execution.Shell environment-specific commands are commands defined in external files that can only be used within the runtime environment of the shell. This include scripts, functions, and modules that add commands to the shell runtime. In PowerShell, these commands added by a module are known as cmdlets (pronounced "command-lets").
Running native commands
Any native command can be run from the PowerShell command line. Usually you run the command exactly
as you would in bash or cmd.exe. The following example shows running the grep command in
bash on Ubuntu Linux.
sdwheeler@circumflex:~$ grep sdwheeler /etc/passwd
sdwheeler:x:1000:1000:,,,:/home/sdwheeler:/bin/bash
sdwheeler@circumflex:~$ pwsh
PowerShell 7.2.6
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
After starting PowerShell on Ubuntu, you can run the same command from the PowerShell command line:
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 command parameters use a forward slash (/) character. Other
command-line tools might use spaces, hyphens, double-dash (--).
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 might need to adjust how you pass those strings.
For more information, see the following articles:
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 nonzero exit code, $? is set to $false. If the exit code is zero,
$? is set to $true.
However, PowerShell 7.2 changed this behavior. 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 how you handle non-zero exit codes from native commands. For more information, see
$PSNativeCommandUseErrorActionPreference.
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 the operator 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 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.