3

The following S.O. question addresses the problem (when you're trying to figure it out from within a script). How can I get the current PowerShell executing file?

How would you do it if you were within a function.

The Example below works outside the definition of the function, just not inside.

echo ''
echo '******** outside function scope'
echo "Path: $($MyInvocation.MyCommand.Path)"
echo "Definition: $($MyInvocation.MyCommand.Definition)"
echo '*******************************'
echo ''

function myHelper()
{
    echo '******** inside function scope'
    #EMPTY
    echo "Path: $($MyInvocation.MyCommand.Path)"
    #Prints the string definition of the function itself
    echo "Definition: $($MyInvocation.MyCommand.Definition)"
    echo '******************************'
}

myHelper
Community
  • 1
  • 1
Jason Jarrett
  • 3,857
  • 1
  • 27
  • 28

1 Answers1

4

You can get this info via:

$MyInvocation.ScriptName

This will return whichever script file the function was invoked from.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369