1

#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
{
    [CmdletBinding()]

    param(
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter(DontShow)]
        $HiddenParameter
    )

    $Name,$HiddenParameter
}

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

DontShowAttr

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
ps
SortOf

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

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

Name
HiddenParameter
(...)

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

Filed in: Columns, Tips and Tricks Tags: , , , , ,

One Response to "#PSTip Hiding parameters from tab completion"

Leave a Reply

Submit Comment

© 4030 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: