Two very little-known or little-used built-in variables are the PSDefaultParameterValues
and PSBoundParameters
dictionaries. Both serve a distinct purpose in scripting, and will make your life easier.
Let's examine the first dictionary, PSDefaultParameterValues
, in the following code sample:
# The automatic variable is great to set defaults $labFolder = mkdir .\StagingDirectory -Force $PSDefaultParameterValues = @{ '*:Path' = $labFolder.FullName # Extremely generic. Set all Path parameters for all Cmdlets 'New-Item:ItemType' = 'File' } # Notice how all cmdlets use Path # New-Item uses ItemType File now New-Item -Name someFile Get-ChildItem Test-Path Join-Path -ChildPath SomePath Get-Item New-FileCatalog -CatalogFilePath here.cat # Cmdlets can still override defaults Add-Content -Path (Join-Path -ChildPath someFile) -Value 'Some data' # Clear the defaults or remove single keys $PSDefaultParameterValues.Remove...