How to Find PowerShell Parameter Default Values

Adam Bertram

Adam Bertram

Read more posts by this author.

You can’t easily find default parameter values for commands in PowerShell but I’ve managed to figure it out in this article.

Today, I had a need to find all of the function parameters that had a default value. After a quick tweet on Twitter, I was advised to use Get-Help using the Parameters property.

(Get-Help Get-FunctionName -Full | select -ExpandProperty parameters).parameter.defaultValue

This works great for functions with help content but didn’t work with my function since it didn’t have any help (gasp!). Instead, I had to use the PowerShell AST to parse out the default parameter default values.

You can see below a simple function called Get-FunctionDefaultParameter. This function performs the following tasks:

  1. Uses Get-Command to pull the code out of a particular function for inspecting.
  2. Looks at the Ast property on the function to get a tree-like structure to parse.
  3. Uses Select-Object‘s calculated properties to do some manipulation on the DefaultValue property to pull out the value.
  4. Uses the FindAll() method on the AST object to find all of the parameters on the function with default values specified.
function Get-FunctionDefaultParameter {
    This is a function that will find all of the default parameter names and values from a given function.
    PS> Get-FunctionDefaultParameter -FunctionName Get-Something
    .PARAMETER FuntionName
    A mandatory string parameter representing the name of the function to find default parameters to.
    try {
        $ast = (Get-Command $FunctionName).ScriptBlock.Ast
        $select = @{ n = 'Name'; e = { $_.Name.VariablePath.UserPath } },
        @{ n = 'Value'; e = { $_.DefaultValue.Extent.Text -replace "`"|'" } }
        $ht = @{ }
        @($ast.FindAll({ $args[0] -is [System.Management.Automation.Language.ParameterAst] }, $true) | Where-Object { $_.DefaultValue } | Select-Object $select).foreach({
                $ht[$_.Name] = $_.Value    
    } catch {
        Write-Error -Message $_.Exception.Message

The AST will natively find the parameters with default values but I had to do a little reengineering with the output. The reason is because the value of $_.DefaultValue.Extent.Text is the raw output which means, if the value is a string you’re going to get single and double quotes there.

I could have chosen to use $_.DefaultValue.Value but if an environment variable like $env:COMPUTERNAME or a scriptblock is specified nothing shows up. So, unfortunately, I had to do a little string replacement. But, it seems to be fine!

Feel free to download this little function from my Gitub repo.

Subscribe to Adam the Automator

Get the latest posts delivered right to your inbox

Looks like you're offline!