Suppose I have a function like this. How can I get a reference to that function and then call it?
function Hello($name) {
Write-Output "Hello, $name"
}
You can do it like this:
function Hello($name) {
Write-Output "Hello, $name"
}
$myFunc = $function:Hello
& $myFunc "Fred"
(& is called the call operator.)
But I think a more idiomatic way in PowerShell is to use a script block:
$Hello = {
param($name)
Write-Output "Hello, $name"
}
& $Hello "Fred"
You can also call the script block like this:
Invoke-Command -ScriptBlock $Hello -ArgumentList "Fred"