Skip to content

Instantly share code, notes, and snippets.

@SteveGilham
Last active August 29, 2015 14:19
Higher-order PowerShell function
# Create a function that accepts a function that takes arguments
Function Test ([scriptblock]$code, $name) {
& $code "$name+$name" | % {
New-Object PSObject -Property @{
Name = $name
Prop = "**$_**"
}
}
}
# Create a function to call
Function Set-Lower ([string]$x) {
$x.ToLowerInvariant()
}
# Create a suitable script block to wrap the function and take a parameter
$b = [scriptblock] { param([string]$Target = "PARAM") Set-Lower $Target }
# Invoke the Test function passing it a scriptblock and argument
Test $b Junku
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment