Skip to content

Instantly share code, notes, and snippets.

@pitermarx
Created August 7, 2018 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitermarx/acfd219c0bb2493fe176cdc9c0e13b9b to your computer and use it in GitHub Desktop.
Save pitermarx/acfd219c0bb2493fe176cdc9c0e13b9b to your computer and use it in GitHub Desktop.
Powershell closures
$Foo = 1
Write-Host "I expect Foo to be 1: " $Foo
function New-Closure {
param([Scriptblock] $Expression, $Foo = 2)
& $Expression
}
New-Closure -Expression {
Write-Host "I expected Foo to be 1, but was 2: " $Foo
}
New-Closure -Expression {
Write-Host "I expected Foo to be 1, and it is: " $Foo
}.GetNewClosure()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment