Exception :
Type : System.Management.Automation.SessionStateUnauthorizedAccessException
ErrorRecord :
Exception :
Type : System.Management.Automation.ParentContainsErrorRecordException
Message : Cannot overwrite variable e because the variable has been optimized. Try using the New-Variable or Set-Variable
cmdlet (without any aliases), or dot-source the command that you are using to set the variable.
HResult : -2146233087
TargetObject : e
CategoryInfo : WriteError: (e:String) [], ParentContainsErrorRecordException
FullyQualifiedErrorId : VariableNotWritableRare
InvocationInfo :
ScriptLineNumber : 5
OffsetInLine : 3
HistoryId : 12
ScriptName : C:\path\to\snippet.ps1
Line : [string] $e = $_
Statement : [string] $e = $_
PositionMessage : At C:\path\to\snippet.ps1:5 char:3
+ [string] $e = $_
+ ~~~~~~~~~~~~~~~~
PSScriptRoot : C:\path\to
PSCommandPath : C:\path\to\snippet.ps1
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, C:\path\to\snippet.ps1: line 5
at <ScriptBlock>, C:\path\to\snippet.ps1: line 4
at <ScriptBlock>, C:\path\to\snippet.ps1: line 1
at <ScriptBlock>, <No file>: line 1
ItemName : e
TargetSite :
Name : SetVariable
DeclaringType : [System.Management.Automation.SessionStateScope]
MemberType : Method
Module : System.Management.Automation.dll
Message : Cannot overwrite variable e because the variable has been optimized. Try using the New-Variable or Set-Variable cmdlet
(without any aliases), or dot-source the command that you are using to set the variable.
Data : System.Collections.ListDictionaryInternal
Source : System.Management.Automation
HResult : -2146233087
StackTrace :
at System.Management.Automation.SessionStateScope.SetVariable(String name, Object value, Boolean asValue, Boolean force,
SessionStateInternal sessionState, CommandOrigin origin, Boolean fastPath)
at System.Management.Automation.SessionStateInternal.SetVariable(VariablePath variablePath, Object newValue, Boolean asValue, Boolean
force, CommandOrigin origin)
at System.Management.Automation.SessionStateInternal.SetVariable(VariablePath variablePath, Object newValue, Boolean asValue,
CommandOrigin origin)
at System.Management.Automation.VariableOps.SetVariableValue(VariablePath variablePath, Object value, ExecutionContext
executionContext, AttributeBaseAst[] attributeAsts)
at System.Management.Automation.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject : e
CategoryInfo : WriteError: (e:String) [], SessionStateUnauthorizedAccessException
FullyQualifiedErrorId : VariableNotWritableRare
InvocationInfo :
ScriptLineNumber : 5
OffsetInLine : 3
HistoryId : 12
ScriptName : C:\path\to\snippet.ps1
Line : [string] $e = $_
Statement : [string] $e = $_
PositionMessage : At C:\path\to\snippet.ps1:5 char:3
+ [string] $e = $_
+ ~~~~~~~~~~~~~~~~
PSScriptRoot : C:\path\to
PSCommandPath : C:\path\to\snippet.ps1
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, C:\path\to\snippet.ps1: line 5
at <ScriptBlock>, C:\path\to\snippet.ps1: line 4
at <ScriptBlock>, C:\path\to\snippet.ps1: line 1
at <ScriptBlock>, <No file>: line 1
Prerequisites
Steps to reproduce
Observed a weird bug, and I'm not quite sure how to accurately describe it, so please feel free to reword the issue title as appropriate 😅.
I'm noticing that when I run this snippet 👇, PowerShell throws an error that reads "Cannot overwrite variable e because the variable has been optimized. Try using the New-Variable or Set-Variable cmdlet (without any aliases), or dot-source the command that you are using to set the variable".
I've also noticed that the error doesn't show up when,
$ewithin theForEach-Objectis not explicitly typed as a[string]; i.e., simply$e = $_instead of[string] $e = $_.foreachmoved above theForEach-Object.Related: the conversation around this message on the PowerShell Discord.
Expected behavior
Actual behavior
Error details
Expand
Environment data
Visuals
No response