Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created January 6, 2025 20:31
Show Gist options
  • Save steviecoaster/99a59c9345324e2d319bd2933116d11b to your computer and use it in GitHub Desktop.
Save steviecoaster/99a59c9345324e2d319bd2933116d11b to your computer and use it in GitHub Desktop.
Get all parameters to function, including default values
Function Get-AllBoundParameters {
[CmdletBinding()]
Param(
# A simple string parameter that is mandatory
[Parameter(Mandatory)]
[String]
$Name,
# A simple switch
[Parameter()]
[Switch]
$Switch,
# A parameter with a default value
[Parameter()]
[String]
$DefaultValue = 'A default'
)
end {
$params = $PSCmdlet.MyInvocation.BoundParameters
$PSCmdlet.MyInvocation.MyCommand.Parameters.Keys | ForEach-Object {
if (-not $params.ContainsKey($_)) {
$params[$_] = (Get-Variable -Name $_ -Scope 0 -ErrorAction SilentlyContinue).Value
}
}
$params
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment