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.
Anyone know offhand how to find all function parameters that have a default value? Get-Command with the Parameters property? #PowerShell
— Adam Bertram (@adbertram) August 25, 2015
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 } }
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?
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.