Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rather better than `$PSBoundParameters`
function Get-ParameterValues {
<#
.Synopsis
Get the actual values of parameters which have manually set (non-null) default values or values passed in the call
.Description
Unlike $PSBoundParameters, the hashtable returned from Get-ParameterValues includes non-empty default parameter values.
NOTE: Default values that are the same as the implied values are ignored (e.g.: empty strings, zero numbers, nulls).
.Example
function Test-Parameters {
[CmdletBinding()]
param(
$Name = $Env:UserName,
$Age
)
$Parameters = . Get-ParameterValues
# This WILL ALWAYS have a value...
Write-Host $Parameters["Name"]
# But this will NOT always have a value...
Write-Host $PSBoundParameters["Name"]
}
#>
[CmdletBinding()]
param(
# The $MyInvocation for the caller -- DO NOT pass this (dot-source Get-ParameterValues instead)
$Invocation = $MyInvocation,
# The $PSBoundParameters for the caller -- DO NOT pass this (dot-source Get-ParameterValues instead)
$BoundParameters = $PSBoundParameters
)
if($MyInvocation.Line[($MyInvocation.OffsetInLine - 1)] -ne '.') {
throw "Get-ParameterValues must be dot-sourced, like this: . Get-ParameterValues"
}
if($PSBoundParameters.Count -gt 0) {
throw "You should not pass parameters to Get-ParameterValues, just dot-source it like this: . Get-ParameterValues"
}
$ParameterValues = @{}
foreach($parameter in $Invocation.MyCommand.Parameters.GetEnumerator()) {
# gm -in $parameter.Value | Out-Default
try {
$key = $parameter.Key
if($null -ne ($value = Get-Variable -Name $key -ValueOnly -ErrorAction Ignore)) {
if($value -ne ($null -as $parameter.Value.ParameterType)) {
$ParameterValues[$key] = $value
}
}
if($BoundParameters.ContainsKey($key)) {
$ParameterValues[$key] = $BoundParameters[$key]
}
} finally {}
}
$ParameterValues
}
@elovelan

This comment has been minimized.

Copy link

@elovelan elovelan commented Oct 10, 2018

@Jaykul, I've been using https://gist.github.com/elovelan/d697882b99d24f1b637c7e7a97f721f2 for a while now (though I just remembered to post it) and it avoids dot-sourcing by using Get-Value -Scope 1 -ValueOnly. Can you see any flaws in my approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment