The command line
PowerShell 7 comes with a module called PSReadLine. A module is a collection of related commands. Modules are explored in greater detail in Chapter 2, Modules.
PSReadLine provides command-line syntax highlighting, preserves history between sessions, and offers completion services when writing commands.
PSReadLine can be configured to offer command completion based on previously typed commands, a useful feature when using similar commands in the console and one that can save searching history for the right command. By default, PSReadline 2.2.6 uses history and a command prediction plugin. The plugin may be explicitly enabled using Set-PSReadLineOption
.
Set-PSReadLineOption -PredictionSource HistoryAndPlugin
Once enabled, PSReadLine will offer suggestions based on typed content that may be completed using Tab as shown in Figure 1.1.
Figure 1.1: PSReadLine Predictive completion
By default, Tab can be used to complete any command or parameter, and a variety of arguments for parameters. In addition to Tab completion, PSReadLine allows the use of Control and Space to provide menu style completion. For example, entering the following partial command:
Get-ChildItem -
Then pressing Control and Space (immediately after the hyphen) will show a menu that can be navigated using the cursor keys, as shown in Figure 1.2:
Figure 1.2: PSReadLine List completion
In PowerShell, the prompt displayed is controlled by a function named prompt. A very simple prompt can be set as shown below:
function prompt {
"$env:USERNAME $pwd PS>"
}
The default prompt can be restored by restarting PowerShell. A profile script is required to make changes on console restart. See about_profiles
for more information:
Get-Help about_profiles
Several modules and tools exist to help customize prompts in PowerShell:
- PowerLine: https://github.com/Jaykul/PowerLine
- oh-my-posh: https://ohmyposh.dev/
- Starship: https://starship.rs/
PowerShell is a complex language; a good editor can save time finding the right syntax to use in a script.