35

For example:

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

Output:

PS > Foo
This function is called foo

4 Answers 4

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

10 Comments

That works - thanks any idea how to get the calling function name? I tried other $myInvocation properties but I don't see one. function foo { 'This function is called {0}.' -f $MyInvocation.MyCommand 'This Caller is called {0}.' -f $MyInvocation.? } function CallFoo { foo } CallFoo
If you are on PowerShell 2.0 use (Get-PSCallStack)[1].Command.
Make that a separate question, so people can find it. The answer in v1 is in gv -sc $_ myinvocation. See jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!172.entry
I've come across two methods of getting the calling function name: 1) (Get-PSCallStack | Select-Object FunctionName -Skip 1 -First 1).FunctionName and 2) (Get-Variable MyInvocation -Scope 1).Value.MyCommand.Name. I tried running each in a loop 1000 times to see how long each took. The Get-Variable... method takes about half the time of the Get-PSCallStack... method.
$MyInvocation cannot be used to get the name of a function within a class. To do this, I needed to use Get-PSCallStack)[0].FunctionName
|
19

Easy.

function Get-FunctionName ([int]$StackNumber = 1) {
    return [string]$(Get-PSCallStack)[$StackNumber].FunctionName
}

By default Get-FunctionName in the example will get the name of the function that called it.

Function get-foo () {
    Get-FunctionName
}
get-foo
#Reutrns 'get-foo'

Increasing the StackNumber parameter will get the name of the next function call up.

Function get-foo () {
    Get-FunctionName -StackNumber 2
}
Function get-Bar  () {
    get-foo 
}
get-Bar 
#Reutrns 'get-Bar'

1 Comment

I'm using this feature in a logging function so being able to chose the $StackNumber to access the name of the caller is practical. Thanks!
12

When you are in a function you can access the automatic variable $PSCmdLet.

This is an extremely useful variable that holds a lot of information about the currently executing cmdlet.

In our scenario we wanted the name and the definition of the current function for some recursion. $MyInvocation was null because the function was within a PowerShell module.

However, there is a "MyInvocation" property on the PSCmdLet object which contains all the information needed and allowed our scenario to run.

e.g. $PSCmdlet.MyInvocation.MyCommand.Name = The name of the function $PSCmdlet.MyInvocation.MyCommand.Definition = The definition of the function

2 Comments

$PSCmdlet appears to only work if the function has an explicit [CmdletBinding()] attribute.
The about_automatic_variables entry for $PsCmdlet says Contains an object that represents the cmdlet or advanced function that is being run. So it only works with advanced functions, which have the CmdletBindingAttribute, not normal functions.
2

The Get-PSCallStack option seems to work only once: when calling a function from the body of the script, the first time it will retrieve the script name, but the second time it will retrieve the text ''

1 Comment

Seems to be repeatable to me, returning the call stack each time I call it. I wanted to find the name of the calling function and using Get-PSCallStack seems to be the way to do it; the method in Jay Bazuzi's link didn't give me the calling function name. For anyone else, I used Get-PSCallStack | Select-Object FunctionName -Skip 1 -First 1 to get the name of the calling function. If the current function is called from the top level script this will return '<ScriptBlock>'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.