As you might know, the tab completion in Powershell 3.0 is much improved. This in part due to the use of expression trees because it is easy to traverse and discover tidbits of information about the command line or script. Some of the really crazy stuff is the ability to tab expand parameter values. Take for example the Get-Process cmdlet. The Get-Process cmdlet has a parameter for the Name of the process. In PowerShell 3.0, it is possible to tab expand the list of running processes.
Get-Process -Name <tab> Get-Process -Name Apoint<tab> Get-Process -Name armsvc<tab>
etc
This is super neat! I had a fellow in my office question whether it was possible to do the same with a custom set of parameter values. It got me pondering. One way to accomplish this is to use the ValidateSetAttribute. Tab expansion will work for that particular parameter.
[ValidateSetAttribute("This","Is","A","Test")]
[Parameter()]
$MyParameter
#...
Get-MyParameter -MyParameter <Tab>
Get-MyParameter -MyParameter This<Tab>
Get-MyParameter -MyParameter Is
etc
But what this fellow was looking to do was something more dynamic: like the processes and services do in PowerShell 3.0. I did some digging into System.Management.Automation with Reflector and found this. There is a CompletionCompleters class (love the name) that contains a method NativeCommandArgumentCompletion. It is responsible for tab expanding a particular collection of “native” PowerShell cmdlets.
There is a real big switch statement that checks to see if the command is of the native set. It also looks like there is no way to extend it.
Maybe someone can prove me wrong. I wish it was a bit more dynamic because it would be pretty neat to be able to add this type of awesome functionality right to our own cmdlets.Maybe I’ll figure out a way to hack that later.
Edit: I’ve been notified that there is a way to extend this. Stay tuned!
It’s pretty interesting how the dynamic value completion works. If you take a look at, for instance, the completion method for services, you will see that it actually calls Get-Service and then sorts it alphabetically. It then returns this as the completion result.
Seeing how this is done, it would seem this would be pretty easy to extend to other cmdlets.
FYI, here is a full list of the supported cmdlets. Each one does the tab expansion a bit different and for different parameters:
- Get-Command
- Show-Command
- Get-Help
- Invoke-Expression
- Clear-EventLog
- Get-EventLog
- Limit-EventLog
- Remove-EventLog
- Write-EventLog
- Get-Job
- Receive-Job
- Remove-Job
- Stop-Job
- Wait-Job
- Suspend-Jpb
- Resume-Job
- Disable-ScheduledJob
- Enabled-ScheduledJob
- Get-ScheduledJob
- Unregister-ScheduledJob
- Get-Module
- Remove-Module
- Import-Module
- Debug-Process
- Get-Process
- Stop-Process
- Wait-Process
- Get-PSDrive
- Remove-PSDrive
- New-PSDrive
- Get-PSProvider
- Get-Service
- Start-Service
- Restart-Service
- Resume-Service
- Set-Service
- Stop-Service
- Suspend-Service
- Clear-Variable
- Get-Variable
- Remove-Variable
- Set-Variable
- Get-Alias
- Get-TraceSource
- Set-TraceSource
- Trace-Command
- Push-Location
- Set-Location
- Move-Item
- Copy-Item










[...] Parameter Value Completion August 1, 2012 robertrieglerwien Leave a comment Go to comments http://csharpening.net/?p=1252 Share this:PrintEmailLike this:LikeBe the first to like this. Categories: Developement Tags: [...]
Hi Adam,
I’m “staying tuned”.. So how do you implement this in custom cmdlets?
Also asked at http://stackoverflow.com/questions/14844542/powershell-cmdlet-parameter-value-tab-completion
-david
Hey David,
I was told the PowerShell team would be outlining this in a blog post. I haven’t seen that blog post. I’ll ping them and see if they have any hints.
That would be great.
Reflecting on System.Management.Automation, I don’t see the ‘CompletionCompleters’ class or a ‘NativeCommandArgumentCompletion’ method any more. I’m guessing you were looking at a pre-release version.
Hey David,
Talked to Bartek at the summit and he pointed me at this: https://github.com/lzybkr/TabExpansionPlusPlus. Jason Shirk from MSFT made it to accommodate this kind of thing. I hope this helps.
Adam
Hi Adam,
thanks, I’ll check it out.
-david