Saturday 3 October 2015

TAB completion in PowerShell

 PowerShell: TAB completion in PowerShell

Reference : PowerShell in Action by Bruce Payette.

One of the interesting feature in Windows PowerShell is the way of TAB completion. TAB completion in PowerShell allows you to partially enter a command and to hit TAB key to get it expanded cmdlet.

PowerShell allows you to do TAB completion against file system, If you type part of a filename or path, a TAB completion can easily be done by hitting TAB key. And the interesting thing is doing TAB completion using wildcard '*' .
C:\ >cd c:\win*ws< tab>
C:\ >cd C:\Windows
And PowerShell can do TAB completion on partial cmdlet names too. If you type a part of a cmdlet and then if pressed TAB, it will give you the first matching expanded cmdlet and continues TAB hit will step through to the possible matches.

We have much more on TAB completion. PowerShell can do TAB completion on Parameters too. Lets see an example.

C:\ >Get-ChildItem -pa <tab>
C:\ >Get-ChildItem -Path
Not only parameters, but on properties too. Have a look on this,
C:\ >$r="A String"
C:\ >$r.Len< tab>

C:\ >$r.Length
The string "A String" is stored in variable $r and then we check the length of the string using Length property. We use TAB completion here by pressing TAB after entering $r.Leng .

We Know PowerShell has a
Get-History cmdlet, Which lists the previously entered cmdlets.
C:\ >Get-History

Id CommandLine
   -- -----------
   23 Clear-History
   24 $r="A String"
   25 $r.Length
Let's do something interesting,
C:\ >#str< tab>
C:\ >$r="A String"
Here we typed #str and hit TAB key , Which returns a previously entered expression that contains 'str' and will step through next matching expression.

We can get the same using the Id of the previously entered expression.
C:\ >#24
C:\ >$r="A String"

To Explore more about Tab completion in PowerShell, Just go through the built-in  PowerShell function TabExpansion which is editable.



No comments:

Post a Comment