#PSTip Hiding parameters from tab completion

Note: This tip requires PowerShell 4.0 or above.

When writing functions and declaring parameters, PowerShell lets us use many parameter attributes to define the behaviour of the parameter. For example, we can decide if the parameter is mandatory, positional, accepts pipeline input and more.

Starting in PowerShell 4.0 there’s a new ParameterAttribute member named DontShow (online docs are not updated yet with that attribute).

PS> New-Object System.Management.Automation.ParameterAttribute

Position                        : -2147483648
ParameterSetName                : __AllParameterSets
Mandatory                       : False
ValueFromPipeline               : False
ValueFromPipelineByPropertyName : False
ValueFromRemainingArguments     : False
HelpMessage                     :
HelpMessageBaseName             :
HelpMessageResourceId           :
DontShow                        : False
TypeId                          : System.Management.Automation.ParameterAttribute

Let’s see how it works. We’ll create a new function with two parameters and try to hide the second one. Notice that we can use a shorthand syntax and not specify a $true value. That’s true for all Boolean parameter (and CmdletBinding) attributes.

function Test-DontShow



Now let’s try and check which parameters are visible to tab completion:

As you can see, tab completion sees only the Name parameter, the HiddenParameter parameter is not visible and is not completable. That said, if you know it’s there you can still operate it:

PS> Test-DontShow -Name ps -HiddenParameter SortOf

Lastly, parameters are still discoverable using Get-Command regardless of their DontShow value.

PS> (Get-Command Test-DontShow).Parameters.Keys


I haven’t found a good reason to hide parameters yet but at least you know you can 🙂

Share on:
comments powered by Disqus