1

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" 
}
0

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.