Retrieving all Named Parameters from Powershell, including empty and set

I am trying to find a way to get all the parameter information from a powershell script. Ex script:

function test() { Param( [string]$foo, [string]$bar, [string]$baz = "baz" ) foreach ($key in $MyInvocation.BoundParameters.keys) { write-host "Parameter: $($key) -> $($MyInvocation.BoundParameters[$key])" } } test -foo "foo!" 

I would like to get the values โ€‹โ€‹of $bar and $baz dynamically without knowing the parameter names ahead of time.

I looked at the properties and methods of $MyInvocation , but I see nothing but the parameters that are set / passed.

Update 1:

I am close to getting it:

 function test() { Param( [string]$foo, [string]$bar, [string]$baz = "baz" ) foreach($var in (get-variable -scope private)) { write-host "$($var.name) -> $($var.value)" } } test -foo "foo!" 

If I could filter out the script options and default options, I would be fine to go.

Update 2: The final working solution is as follows:

 function test { param ( [string] $Bar = 'test' , [string] $Baz , [string] $Asdf ) $ParameterList = (Get-Command -Name $MyInvocation.InvocationName).Parameters; foreach ($key in $ParameterList.keys) { $var = Get-Variable -Name $key -ErrorAction SilentlyContinue; if($var) { write-host "$($var.name) > $($var.value)" } } } test -asdf blah; 
+12
parameters powershell
source share
7 answers

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

  • Dynamically get command name using $PSCmdlet
  • Get a list of options for a command using Get-Command
  • Examine the value of each parameter using the Get-Variable cmdlet.

the 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; 

Exit

The result of the command is as follows:

 Name Value ---- ----- Bar test Baz Asdf blah 
+22
source share

To dynamically read a value, use the get-variable / cmdlet function

 write-host (get-variable "foo") 

To print all settings, follow these steps:

 foreach ($key in $MyInvocation.BoundParameters.keys) { $value = (get-variable $key).Value write-host "$key -> $value" } 
+12
source share

I found this most useful for PS4 (Windows 2012 R2) - it includes default values โ€‹โ€‹/ optional parameters:

 $cmdName = $MyInvocation.InvocationName $paramList = (Get-Command -Name $cmdName).Parameters foreach ( $key in $paramList.Keys ) { $value = (Get-Variable $key -ErrorAction SilentlyContinue).Value if ( $value -or $value -eq 0 ) { Write-Host "$key -> $value" } } 
+4
source share

Hopefully some may find this one line file useful:

 function test() { Param( [string]$foo, [string]$bar, [string]$baz = "baz" ) $MyInvocation.MyCommand.Parameters | Format-Table -AutoSize @{ Label = "Key"; Expression={$_.Key}; }, @{ Label = "Value"; Expression={(Get-Variable -Name $_.Key -EA SilentlyContinue).Value}; } } test -foo "foo!" 

Result

 Keys Value ---- ----- foo foo! bar baz baz 
+2
source share

I played with two solutions that I liked in this thread, they both work. however I needed to create an error when skipping a parameter to build the script

  $cmdName = $MyInvocation.InvocationName $paramList = (Get-Command -Name $cmdName).Parameters foreach ( $key in $paramList.Keys ) { $value = (Get-Variable $key -ErrorAction Stop) #Write-Host $value.Value #remove comment for error checking if ([string]::IsNullOrEmpty($value.Value)){ $(throw ("$key is a mandatory value please declare with -$key <Required value> " )) } } 
+1
source share
  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; $parameterobj=$ParameterList.Values foreach($parameter in $parameterobj ) { #Write-Host $pr.Name Get-Variable -Name $parameter.Name -ErrorAction SilentlyContinue; } } cls test -asdf blah; 

it worked for me.

0
source share

For those of you who don't want to use cmdletbinding (), there is a single-liner option that I found above:

 (Get-Command -Name $PSCommandPath).Parameters | Format-Table -AutoSize @{ Label = "Key"; Expression={$_.Key}; }, @{ Label = "Value"; Expression={(Get-Variable -Name $_.Key -EA SilentlyContinue).Value}; } 

$ PSCommandPath is always available

0
source share

All Articles