Prerequisites
Summary
Variables initialized in begin block of ForEach-Object trigger PSUseDeclaredVarsMoreThanAssignments warning.
PowerShell Version
Name Value
---- -----
PSVersion 5.1.26100.7705
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.26100.7705
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Name : ConsoleHost
Version : 5.1.26100.7705
InstanceId : 4ed60023-e437-45ae-b589-226f399acc62
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
Visual Studio Code Version
1.109.5
072586267e68ece9a47aa43f8c108e0dcbf44622
x64
Extension Version
ms-vscode.powershell@2025.4.0
Steps to Reproduce
1, 2, 3 | ForEach-Object { $list = @() } { $list += $_ } { $list }
^^^^^
The variable 'list' is assigned but never used.
PSScriptAnalyzer(PSUseDeclaredVarsMoreThanAssignments)
Returning the $list from the begin block offs the warning
1, 2, 3 | ForEach-Object { $list = @(); $list } { $list += $_ } { $list }
Visuals
No response
Logs
No response
Prerequisites
Summary
Variables initialized in
beginblock ofForEach-ObjecttriggerPSUseDeclaredVarsMoreThanAssignmentswarning.PowerShell Version
Visual Studio Code Version
Extension Version
ms-vscode.powershell@2025.4.0Steps to Reproduce
Returning the
$listfrom the begin block offs the warningVisuals
No response
Logs
No response