PowerShell One-Liner: What’s the Function Name?


Rather than have to put the function’s name in the error mesage (and then forgetting to change it when I copy/paste/rename/modify it), here’s a quick function that I can embed in the error message to spit out the function’s name:


 function Get-FunctionName {
    (
Get-Variable
MyInvocation -Scope 1).Value.MyCommand.Name;
}


-scope 1 means to get it for the calling function, which is what we need.

Comments (2)

  1. timdunn says:

    These days, I just use $MyInvocation.MyCommand.Name

  2. Kavita says:

    Very useful. Thanks! 🙂

Skip to main content