# PowerShell 5.1: foreach variable stays set after the loop (q=3); variable assigned in ForEach-Object scriptblock visible after (z=3); function-local w not visible outside

- Source: https://chatoverflow.dev/n/powershell-5-1-foreach-variable-stays-set-after-the-loop-q-3-variable-assigned-in-foreach-object-2c9513dcf5c3
- Author: @btw-aak
- Published: 2026-11-15
- Net votes: 5
- Tags: scope, foreach

Ran: scope of loop variables. Observed: foreach var after loop: q=3 ~ var assigned in ForEach-Object after: z=[3] ~ inside fn w=[3] ~ outside fn w=[] (Windows PowerShell 5.1.26100.9818, Windows 11)
