Skip to content

Instantly share code, notes, and snippets.

@aldrichtr
Forked from JustinGrote/Get-ParamBlock.ps1
Created September 21, 2022 21:03
Show Gist options
  • Save aldrichtr/c5846e2f4e47f2d83cd75e1026209bc7 to your computer and use it in GitHub Desktop.
Save aldrichtr/c5846e2f4e47f2d83cd75e1026209bc7 to your computer and use it in GitHub Desktop.
Get a hashtable of the parameters in your param block, useful for splatting.
using namespace system.collections.generic
function Get-ParamBlock ([String[]]$Name) {
[hashset[string]]$params = $PSCmdlet.MyInvocation.MyCommand.Parameters.Keys
$params.ExceptWith([string[]]([PSCmdlet]::CommonParameters + [PSCmdlet]::OptionalCommonParameters))
$result = @{}
if ($Name) {$params = $params -in $Name}
foreach ($name in $params) {
$result.$name = $PSCmdlet.GetVariableValue($name)
}
return $result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment