PowerShell Closures

25 May 2012 12:07 powershell
# Create a script block. It doesn't capture $x.
$x = 'A'
$sb = { Write-Host $x }

# Change the value of $x and run the script block. Note that it displays the current value.
$x = 'B'
& $sb                  # ... B

# Create a closure.
$closure = $sb.GetNewClosure()

# Change the value of $x.
$x = 'C'

# Run the closure. It displays the original value. Success.
& $closure             # ... B

# For comparison, run the script block. Nope. Still tracking the current value.
& $sb                  # ... C