Repository: PowerShell/PowerShell
File: src/Microsoft.PowerShell.Commands.Utility/commands/utility/Var.cs (C#)

Code before:
            if (Value != AutomationNull.Value)
            {
                _valueIsFormalParameter = true;
            }
        }

        /// <summary>
        /// If name and value are both specified as a formal parameters, then
        /// just ignore the incoming objects in ProcessRecord.
        /// If name is a formal parameter but the value is coming from the pipeline,
        /// then accumulate the values in the valueList and set the variable during
        /// EndProcessing().
        /// If name is not a formal parameter, then set
        /// the variable each time ProcessRecord is called.
        /// </summary>
        protected override void ProcessRecord()
        {
            if (_nameIsFormalParameter && _valueIsFormalParameter)
            {
                return;
            }

            if (_nameIsFormalParameter && !_valueIsFormalParameter)
            {
                if (Value != AutomationNull.Value)
                {
                    _valueList ??= new List<object>();

                    _valueList.Add(Value);
                }
            }
            else
            {
                SetVariable(Name, Value);
            }
        }

        private List<object> _valueList;

        /// <summary>
        /// Sets the variable if the name was specified as a formal parameter
        /// but the value came from the pipeline.
        /// </summary>
        protected override void EndProcessing()
        {
            if (_nameIsFormalParameter)
            {
                if (_valueIsFormalParameter)
                {
                    SetVariable(Name, Value);
                }

Review comment (suggestion):
```suggestion
                PSVariable currentValue = Context.SessionState.PSVariable.Get(Name[0]);
```

Code after:
        protected override void BeginProcessing()
        {
            if (Name != null && Name.Length > 0)
            {
                _nameIsFormalParameter = true;
            }

            if (Value != AutomationNull.Value)
            {
                _valueIsFormalParameter = true;
            }

            if (Append)
            {
                // create the list here and add to it if it has a value
                // but if they have more than one name, produce an error
                if (Name.Length != 1)
                {
                    ErrorRecord appendVariableError = new ErrorRecord(new InvalidOperationException(), "SetVariableAppend", ErrorCategory.InvalidOperation, Name);
                    appendVariableError.ErrorDetails = new ErrorDetails("SetVariableAppend");
                    appendVariableError.ErrorDetails.RecommendedAction = VariableCommandStrings.UseSingleVariable;
                    ThrowTerminatingError(appendVariableError);
                }

                _valueList = new List<object>();
                var currentValue = Context.SessionState.PSVariable.Get(Name[0]);
                if (currentValue is not null)
                {
                    if (currentValue.Value is IList<object> ilist)
                    {
                        _valueList.AddRange(ilist);
                    }
                    else
                    {
                        _valueList.Add(currentValue.Value);
                    }
                }
            }
        }

        /// <summary>
        /// If name and value are both specified as a formal parameters, then
        /// just ignore the incoming objects in ProcessRecord.
        /// If name is a formal parameter but the value is coming from the pipeline,
        /// then accumulate the values in the valueList and set the variable during
        /// EndProcessing().
        /// If name is not a formal parameter, then set
        /// the variable each time ProcessRecord is called.
        /// </summary>
        protected override void ProcessRecord()
        {