How to get current executable in powershell function

The following SO question solves the problem (when you try to understand it from a script). How to get the current PowerShell executable?

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

The example below works outside the function definition, 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
+3
source share
1 answer

You can get this information through:

$MyInvocation.ScriptName

This will return any script file with which the function was called.

+4
source

Source: https://habr.com/ru/post/1764053/


All Articles