Twitter Reply – Finding Parameters that Include a Default Value

Adam Bertram, an active PowerShell MVP, wrote on Twitter about the need to determine which parameters have default values. I can’t resist a challenge, and so I very quickly wrote out the function below. No guarantees, but maybe it’ll help.

Update: It turns out that the function that Adam was working with, didn’t include help. Shame. Even so, this small script may assist someone to pull back the parameters that have default values.

Get-Help Get-Service |
    Select-Object -ExpandProperty parameters |
    Select-Object -ExpandProperty parameter | 

    Foreach {
        If ($_.DefaultValue -ne '') {
            $Object = [pscustomobject]@{
                Name = $_.Name
                Default =$_.DefaultValue
            }
        Write-Output -InputObject $Object
        }
    }

2 thoughts on “Twitter Reply – Finding Parameters that Include a Default Value

  1. Mike Shepard

    This is good. Just a style question about why you used $object = followed by Write-output when you could have just had the [PSCustomObject] inside the if and been fine?

    Reply
  2. tommymaynard Post author

    I think that was likely due to habit, more than anything else. You are right, though. I could have easily skipped writing the object to a variable, $Object, and forgone the need to use the Write-Output cmdlet. I tried it your way, and honestly, I’m not sure I’ve ever done it that way before. Good to know that option exists. Unless I can find a reason that doesn’t work in the future, I rather like saving those two lines.

    Reply

Leave a Reply

Your email address will not be published.