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 parameters.

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.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!