Free preview.You're sampling one lesson — enroll free to unlock all 10 lessons and track your progress.
Enroll free lesson
Closures and Higher-Order Functions
Closures and Higher-Order Functions
In this lesson — part of Advanced Features — you'll learn closures and higher-order functions in PowerShell and why it matters in real work.
Why it matters
Functions that take or return functions unlock concise, composable code.
Key ideas
- Functions as values
- map / filter / reduce
- Callbacks
- Composition
In practice
Here's how it looks in idiomatic PowerShell:
function Invoke-Twice {
param([scriptblock]$Action, $Value)
& $Action $Value
& $Action $Value
}
$double = { param($x) $x * 2 }
Invoke-Twice -Action $double -Value 5 # 10, 10
1..3 | ForEach-Object $double # pass a scriptblock as the transform
[Func[int,int]]$f = { param($x) $x + 1 } # interop with .NET delegates
PowerShell note: Script blocks ({ }) are first-class values you store in variables and invoke with the call operator (&), and they implicitly convert to .NET delegate types like Func<T> for interop with framework APIs.
Try it yourself
Exercise: In PowerShell, use map and filter to get the squares of the even numbers.
Recap
You now understand closures and higher-order functions and can apply it in PowerShell. Mark this lesson complete and continue to the next one.
