Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the currently executing file within powershell function

Tags:

powershell

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
like image 854
Jason Jarrett Avatar asked Apr 01 '26 02:04

Jason Jarrett


1 Answers

You can get this info via:

$MyInvocation.ScriptName

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

like image 127
Keith Hill Avatar answered Apr 02 '26 23:04

Keith Hill