Last active
September 25, 2024 18:16
-
-
Save realslacker/2d6ececb97d489a9ed62b6e0df3eb699 to your computer and use it in GitHub Desktop.
Get list of Common Parameters in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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