Skip to content

Instantly share code, notes, and snippets.

@realslacker
Last active September 25, 2024 18:16
Show Gist options
  • Save realslacker/2d6ececb97d489a9ed62b6e0df3eb699 to your computer and use it in GitHub Desktop.
Save realslacker/2d6ececb97d489a9ed62b6e0df3eb699 to your computer and use it in GitHub Desktop.
Get list of Common Parameters in PowerShell
# if you want to get a list to use in a script
# by wrapping in a script block you don't pollute the current namespace
$CommonParameters = {
function Get-CommonParameters {
[CmdletBinding( SupportsShouldProcess=$true )]
param()
return $MyInvocation.MyCommand.Parameters.Keys
}
Get-CommonParameters
}.Invoke()
# you might use this if you want to strip all common parameters off
# $PSBoundParameters
function Get-TestParameters {
[CmdletBinding( SupportsShouldProcess=$true )]
param(
$TestBoundParameter
)
$Script:CommonParameters | ForEach-Object {
$PSBoundParameters.Remove($_) > $null
}
return $PSBoundParameters
}
# this should output @{ TestBoundParameter = 'TestValue' }
Get-TestParameters -Verbose -Confirm -ErrorAction SilentlyContinue -TestBoundParameter 'TestValue'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment