public/Remove-ParameterDefault.ps1
|
<#
.SYNOPSIS Removes a value that would have been used for a parameter if none was specified, if one existed. .INPUTS An object with a ParameterName property that identifies a property to remove a default for. .FUNCTIONALITY Parameters .LINK Stop-ThrowError .LINK Get-Command .LINK about_Scopes .EXAMPLE Remove-ParameterDefault epcsv nti -Scope Global Establishes that the -NoTypeInformation param of the Export-Csv cmdlet will revert to false (as established by the cmdlet) if not otherwise specified, globally for the PowerShell session. .EXAMPLE Remove-ParameterDefault Select-Xml Namespace Removes any namespaces used by Select-Xml when none are given explicitly. #> #Requires -Version 3 [CmdletBinding()] Param( # The name of a cmdlet, function, script, or alias to remove a default parameter value from. [Parameter(Position=0,Mandatory=$true)][ValidateNotNullOrEmpty()][Alias('CmdletName')][string] $CommandName, # The name or alias of the parameter to remove a default value from. [Parameter(Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true)][ValidateNotNullOrEmpty()][string] $ParameterName, # The scope of this default. [string] $Scope = 'Local' ) Begin { $Scope = Add-ScopeLevel $Scope $cmd = Get-Command $CommandName -ErrorAction Ignore if(!$cmd) {Stop-ThrowError "Could not find command '$CommandName'" -Argument CommandName} if($cmd.CommandType -eq 'Alias') {$cmd = Get-Command $cmd.ResolvedCommandName} if($cmd.CommandType -notin 'Cmdlet','ExternalScript','Function','Script') {Stop-ThrowError "Command '$CommandName' ($($cmd.CommandType)) not supported" -Argument CommandName} $defaults = Get-Variable PSDefaultParameterValues -Scope $Scope -ErrorAction Ignore } Process { if(!$defaults) {return} $name = try {"$($cmd.Name):$($cmd.ResolveParameter($ParameterName).Name)"} catch {Stop-ThrowError "Could not find parameter '$ParameterName' for cmdlet '$CommandName'" -Argument ParameterName} Write-Verbose "Removing default parameter '$name'" if($defaults.Value.ContainsKey($name)) {$defaults.Value.Remove($name)} } |