Alex Rivera | Logout

Is there a way to retrieve a PowerShell function name from within a function?

Asked 2010-09-11T02:25:48.603
35

For example:

function Foo { 
    [string]$functionName = commandRetrievesFoo
    Write-Host "This function is called $functionName"
}

Output:

PS > Foo
This function is called foo
Edit
Report

1 Answer

59

You can use $MyInvocation which contains some useful information about what is currently executed.

function foo {
    'This function is called {0}.' -f $MyInvocation.MyCommand
}
answered 2010-09-11T11:34:28.840

Your Answer