$MyInvocation.MyCommand.Path returning NULL
$MyInvocation
is an automatic variable populated at script run time, then if you execute $MyInvocation.MyCommand.Path
in a powershell console
or ISE
isn't populated;
that's why in your test the $ScriptPath
has no value ($null
)