-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Run in any PowerShell 7+ session
Clear-Variable *
Expected behavior
Clear-Variable * should automatically skip all variables with ReadOnly or Constant options set. System variables like $true, $false, $Error, and $PSStyle should be silently filtered out before any clear operation is attempted. Only user-defined, modifiable variables should be processed.
Actual behavior
The cmdlet attempts to clear ALL variables including immutable system constants and automatic variables, resulting in predictable and non-actionable errors:
Clear-Variable: Cannot overwrite variable false because it is read-only or constant.
Clear-Variable: Cannot overwrite variable true because it is read-only or constant.
Clear-Variable: Cannot overwrite variable Error because it is read-only or constant.
Clear-Variable: Cannot overwrite variable PSStyle because it is read-only or constant.
Clear-Variable: Cannot convert null to type "System.Management.Automation.ConfirmImpact" due to enumeration values that are not valid. Specify one of the following enumeration values and try again. The possible enumeration values are "None,Low,Medium,High".
Error details
Get-Error | Select-Object -First 1 | Format-List *
irst 1 | Format-List *Clear-Variable:
Line |
5 | Clear-Variable *
| ~~~~~~~~~~~~~~~~
| Cannot overwrite variable false because it is read-only or constant.
Clear-Variable:
Line |
5 | Clear-Variable *
| ~~~~~~~~~~~~~~~~
| Cannot overwrite variable true because it is read-only or constant.
Clear-Variable:
Line |
5 | Clear-Variable *
| ~~~~~~~~~~~~~~~~
| Cannot overwrite variable Error because it is read-only or constant.
Clear-Variable:
Line |
5 | Clear-Variable *
| ~~~~~~~~~~~~~~~~
| Cannot overwrite variable PSStyle because it is read-only or constant.
Clear-Variable:
Line |
5 | Clear-Variable *
| ~~~~~~~~~~~~~~~~
| Cannot convert null to type "System.Management.Automation.ConfirmImpact" due to enumeration values that are not valid. Specify one of the following enumeration values and try again. The possible enumeration values are "None,Low,Medium,High".
Exception : System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert null to type "System.Management.Automation.ConfirmImpact" due to enumeration values that are not valid. S
pecify one of the following enumeration values and try again. The possible enumeration values are "None,Low,Medium,High".
---> System.Management.Automation.PSInvalidCastException: Cannot convert null to type "System.Management.Automation.ConfirmImpact" due to enumeration values that are not valid. Specify one
of the following enumeration values and try again. The possible enumeration values are "None,Low,Medium,High".
at System.Management.Automation.LanguagePrimitives.ThrowInvalidCastException(Object valueToConvert, Type resultType)
at System.Management.Automation.LanguagePrimitives.ConvertNoConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProv
ider, TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData, Boolean bindingParameters, Boolean bindingScriptCmdlet)
--- End of inner exception stack trace ---
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData, Boolean bindingParameters, Boolean bindingScriptCmdlet)
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData)
at System.Management.Automation.PSVariable.TransformValue(IEnumerable`1 attributes, Object value)
at System.Management.Automation.PSVariable.SetValue(Object value)
at System.Management.Automation.SessionStateScope.SetVariable(String name, Object value, Boolean asValue, Boolean force, SessionStateInternal sessionState, CommandOrigin origin, Boolean f
astPath)
at System.Management.Automation.SessionStateInternal.SetVariable(VariablePath variablePath, Object newValue, Boolean asValue, Boolean force, CommandOrigin origin)
at System.Management.Automation.SessionStateInternal.SetVariableValue(String name, Object newValue, CommandOrigin origin)
at Microsoft.PowerShell.Commands.ClearVariableCommand.ClearValue(PSVariable matchingVariable)
at Microsoft.PowerShell.Commands.ClearVariableCommand.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject :
CategoryInfo : MetadataError: (:) [Clear-Variable], ArgumentTransformationMetadataException
FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.ClearVariableCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 5
PipelineIterationInfo : {}
Exception : System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert null to type "System.Management.Automation.ConfirmImpact" due to enumeration values that are not valid. S
pecify one of the following enumeration values and try again. The possible enumeration values are "None,Low,Medium,High".
---> System.Management.Automation.PSInvalidCastException: Cannot convert null to type "System.Management.Automation.ConfirmImpact" due to enumeration values that are not valid. Specify one
of the following enumeration values and try again. The possible enumeration values are "None,Low,Medium,High".
at System.Management.Automation.LanguagePrimitives.ThrowInvalidCastException(Object valueToConvert, Type resultType)
at System.Management.Automation.LanguagePrimitives.ConvertNoConversion(Object valueToConvert, Type resultType, Boolean recurse, PSObject originalValueToConvert, IFormatProvider formatProv
ider, TypeTable backupTable)
at System.Management.Automation.LanguagePrimitives.ConvertTo(Object valueToConvert, Type resultType, Boolean recursion, IFormatProvider formatProvider, TypeTable backupTypeTable)
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData, Boolean bindingParameters, Boolean bindingScriptCmdlet)
--- End of inner exception stack trace ---
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData, Boolean bindingParameters, Boolean bindingScriptCmdlet)
at System.Management.Automation.ArgumentTypeConverterAttribute.Transform(EngineIntrinsics engineIntrinsics, Object inputData)
at System.Management.Automation.PSVariable.TransformValue(IEnumerable`1 attributes, Object value)
at System.Management.Automation.PSVariable.SetValue(Object value)
at System.Management.Automation.SessionStateScope.SetVariable(String name, Object value, Boolean asValue, Boolean force, SessionStateInternal sessionState, CommandOrigin origin, Boolean f
astPath)
at System.Management.Automation.SessionStateInternal.SetVariable(VariablePath variablePath, Object newValue, Boolean asValue, Boolean force, CommandOrigin origin)
at System.Management.Automation.SessionStateInternal.SetVariableValue(String name, Object newValue, CommandOrigin origin)
at Microsoft.PowerShell.Commands.ClearVariableCommand.ClearValue(PSVariable matchingVariable)
at Microsoft.PowerShell.Commands.ClearVariableCommand.ProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject :
CategoryInfo : MetadataError: (:) [Clear-Variable], ArgumentTransformationMetadataException
FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.ClearVariableCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 5
PipelineIterationInfo : {}
Environment data
$PSVersionTable
Name Value
---- -----
PSVersion 7.5.2
PSEdition Core
GitCommitId 7.5.2
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
Get-Variable true, false, Error, PSStyle | Select-Object Name, Options
Let's check what variables DON'T have ReadOnly or Constant set
Get-Variable * | Where-Object {
-not ($.Options -band [System.Management.Automation.ScopedItemOptions]::ReadOnly) -and
-not ($.Options -band [System.Management.Automation.ScopedItemOptions]::Constant)
} | Select-Object Name, OptionsOr more clearly, let's see which variables are causing issues
Get-Variable * | Where-Object {
$_.Options -match 'ReadOnly|Constant'
} | Select-Object Name, OptionsFind variables that might have the ConfirmImpact type
Get-Variable * | Where-Object {
$_.Value -is [System.Management.Automation.ConfirmImpact]
} | Select-Object Name, Value, Options
Name Options
true Constant, AllScope
false Constant, AllScope
Error Constant
PSStyle Constant
^ None
$ None
args None
AvailableLayers None
ConfirmPreference None
DebugPreference None
ErrorActionPreference None
ErrorView None
FormatEnumerationLimit None
InformationPreference None
input None
Matches None
MaximumHistoryCount None
MyInvocation None
NestedPromptLevel None
null None
OutputEncoding None
PROFILE None
ProgressPreference None
PSBoundParameters None
PSCommandPath None
PSDefaultParameterValues None
PSEmailServer None
PSNativeCommandArgumentPassing None
PSNativeCommandUseErrorActionPreference None
PSScriptRoot None
PSSessionApplicationName None
PSSessionConfigurationName None
PSSessionOption None
PWD None
StackTrace None
VerbosePreference None
WarningPreference None
WhatIfPreference None
? ReadOnly, AllScope
EnabledExperimentalFeatures Constant, AllScope
Error Constant
ExecutionContext Constant, AllScope
false Constant, AllScope
HOME ReadOnly, AllScope
Host Constant, AllScope
IsCoreCLR ReadOnly, AllScope
IsLinux ReadOnly, AllScope
IsMacOS ReadOnly, AllScope
IsWindows ReadOnly, AllScope
PID Constant, AllScope
PSCulture ReadOnly, AllScope
PSEdition Constant, AllScope
PSHOME Constant, AllScope
PSStyle Constant
PSUICulture ReadOnly, AllScope
PSVersionTable Constant, AllScope
ShellId Constant, AllScope
true Constant, AllScope
ConfirmPreference None