#PSTip Argument disambiguation in PowerShell 3.0

Note: This tip requires PowerShell 3.0 or above.

When writing commands in PowerShell you can be as formal as:

Get-ChildItem -Include*.txt -Recurse

Or you can be as terse as:

gci -i *.txt -r

As you can see you use aliases (gci) instead of writing the full command name and you can also use partial names of parameters (i,r). Shortened parameter names are allowed as long as you have specified enough of the parameter name to make it unique.

For example, what would happen if we specify -f to Get-ChildItem:

PS> gci -f *.txt
Get-ChildItem : Parameter cannot be processed because the parameter name 'f' is ambiguous.
Possible matches include: -Filter -Force.

As you can see, specifying just ‘f’ is not enough as it yields two matches, so the parameter binder can’t decide which one to use. To disambiguate it we need to add more characters; in this case ‘fi’ would suffice.

In PowerShell 3.0 we can also use the same technique to shorten parameter arguments. This works as long as the parameter type is an Enum object. So, instead of writing:

PS> Write-Host hello -ForegroundColor Yellow

We can now do:

PS> Write-Host hello -ForegroundColor y

If you try to use ‘b’ for example, you’ll get an error that says it all:

PS> Write-Host hello -ForegroundColor b
Write-Host : Cannot bind parameter 'ForegroundColor'. Cannot convert value "b" to type "System.ConsoleColor". Error:
"The identifier name b cannot be processed due to the inability to differentiate between the following enumerator
names: Black, Blue. Try a more specific identifier name."
Share on: