Getting all Named Parameters from Powershell including empty and set ones

Check this solution out. This uses the CmdletBinding() attribute, which provides some additional metadata through the use of the $PSCmdlet built-in variable. You can:

  1. Dynamically retrieve the command’s name, using $PSCmdlet
  2. Get a list of the parameter for the command, using Get-Command
  3. Examine the value of each parameter, using the Get-Variable cmdlet

Code:

function test {
    [CmdletBinding()]
    param (
          [string] $Bar="test"
        , [string] $Baz
        , [string] $Asdf
    )
    # Get the command name
    $CommandName = $PSCmdlet.MyInvocation.InvocationName;
    # Get the list of parameters for the command
    $ParameterList = (Get-Command -Name $CommandName).Parameters;

    # Grab each parameter value, using Get-Variable
    foreach ($Parameter in $ParameterList) {
        Get-Variable -Name $Parameter.Values.Name -ErrorAction SilentlyContinue;
        #Get-Variable -Name $ParameterList;
    }
}

test -asdf blah;

Output

The output from the command looks like this:

Name                           Value                                           
----                           -----                                           
Bar                            test                                            
Baz                                                                            
Asdf                           blah                                            

Leave a Comment