Muokkaa

Jaa


about_Tab_Expansion

Short description

PowerShell provides completions on input to provide hints, enable discovery, and speed up input entry. Command names, parameter names, argument values and file paths can all be completed by pressing the Tab key.

Long description

Tab expansion is controlled by the internal function TabExpansion2. Since this function can be modified or overridden, this discussion is a guide to the behavior of the default PowerShell configuration.

Tab expansion behavior can also be modified by the Predictive IntelliSense feature of the PSReadLine module. For more information, see Predictive IntelliSense.

The Tab key is the default key binding on Windows. You can change the keybinding using the PSReadLine module or the application that's hosting PowerShell. The keybinding is different on non-Windows platforms. For more information, see about_PSReadLine.

Note

One limitation of the tab expansion process is that tabs are always interpreted as attempts to complete a word. If you copy and paste command examples into a PowerShell console, make sure that the sample doesn't contain tabs. If it does, the results will be unpredictable and will almost certainly not be what you intended.

File and cmdlet name completion

To fill in a filename or path from the available choices automatically, type part of the name and press the Tab key. PowerShell automatically expands the name to the first match that it finds. Pressing the Tab key repeatedly cycles through all the available choices.

The tab expansion of cmdlet names is slightly different. To use tab expansion on a cmdlet name, type the entire first part of the name (the verb) and the hyphen that follows it. You can fill in more of the name for a partial match. For example, if you type get-co and then press the Tab key, PowerShell automatically expands this to the Get-Command cmdlet. Notice that it also changes the case of letters to their standard form. If you press Tab key again, PowerShell replaces this with the only other matching cmdlet name, Get-Content.

Note

As of PowerShell 7.0, Tab also expands abbreviated cmdlets and functions. For example, i-psdf<tab> returns Import-PowerShellDataFile.

Tab completion also works to resolve PowerShell alias and native executables.

You can use tab expansion repeatedly on the same line. For example, you can use tab expansion on the name of the Get-Content cmdlet by entering:

Examples

PS> Get-Con<Tab>

When you press the Tab key, the command expands to:

PS> Get-Content

You can then partially specify the path to the Active Setup log file and use tab expansion again:

PS> Get-Content c:\windows\acts<Tab>

When you press the Tab key, the command expands to:

PS> Get-Content C:\windows\actsetup.log

PSReadLine also has a menu completion feature. The default key binding on Windows is Ctrl+Space.

PS> fore<Ctrl-Space>

When you press Ctrl+Space, PowerShell presents the full list of matching values as a menu:

PS> foreach
foreach         ForEach-Object  foreach.cmd

In this example the string 'fore' is matched to foreach (PowerShell alias), ForEach-Object (cmdlet), and foreach.cmd (native command). Use the arrow keys to select the value you want.

Parameter argument completion

Tab completion can also work to complete parameter arguments. You can use the Tab key to cycle through a list of possible values that are valid for some parameter. For more information, see about_Functions_Argument_Completion.

Enumerated value completion

PowerShell 7.0 added support for tab completion of enums. You can use tab completion to select the value you want anywhere you use an enum. For example:

enum Suits {
    Clubs = 0
    Diamonds = 1
    Hearts = 2
    Spades = 3
}

[Suits]$suit = 'c<Tab>

Enumerated values are strings, so the value to be completed must start with a single or double-quote character.

When you hit the Tab key, you get the following results:

[Suits]$suit = 'Clubs'

Tab completion also works with .NET enumerations.

[System.IO.FileAttributes]$attr = 'S<Tab><Tab>

Hitting the Tab key twice cycles through the two values that start with the letter S. The end result is:

[System.IO.FileAttributes]$attr = 'System'

Beginning in PowerShell 7.0, tab expansion was added for the values of ValidateSet when assigning to a variable. For example, if you were typing the following variable definition:

[ValidateSet('Chocolate', 'Strawberry', 'Vanilla')]
[string]$flavor = 'Strawberry'
$flavor = <tab>

When you hit the Tab key, you would get the following result:

$flavor = 'Chocolate'

Tab completions for comment-based keywords

Beginning in PowerShell 7.2, support was added for tab completion of the #requires parameters and the keywords for comment-based help.

Example for #requires statement

#requires -<Ctrl-Space>

Menu expansion shows the following parameter options:

#requires -<Ctrl-Space>
Modules     PSEdition     RunAsAdministrator    Version

Example for comment-based help

<#
    .<Ctrl-Space>

Menu expansion shows the following keyword options:

 <#
    .COMPONENT
COMPONENT      EXTERNALHELP           FUNCTIONALITY     NOTES         REMOTEHELPRUNSPACE
DESCRIPTION    FORWARDHELPCATEGORY    INPUTS            OUTPUTS       ROLE
EXAMPLE        FORWARDHELPTARGETNAME  LINK              PARAMETER     SYNOPSIS

See also