This will be a short post. Now and again I find myself needing to know the names of the common parameters that is added to an advanced function. Usually this is because I want to use splatting and want to remove any usage of those built-in parameters. Here is how you can do it.
This time I was working on an advanced function that had a lot of parameters, counting in at 34 to be more precise. There was also a nice mixture of default values and calculated values that required my attention. The function should output those parameters with their values like so:
-Param1 <value>
-Param2 <value>
Now I could use $PSboundparameters, however that would not include the parameters with default values, hence I went the other way and used $MyInvocation.MyCommand.Parameters. This will give me a dictionary list of all the parameters including the common parameters (see help about_commonparameters) and those have to be removed.
A couple of days ago I was browsing around the [System.Management.Automation] and [Microsoft.Powershell] .Net classes looking for something else and I found this cute little static property:
[System.Management.Automation.Cmdlet]::CommonParameters
Have been looking for this little bugger for some time now, really nice to find it! Armed with this it is quite easy to remove the CommonParameters from my list of parameters.
That was nice!
Cheers
Tore
Comments
Post a Comment