3

I created my ps1 script and to get the path dynamically I used this command

$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

Everything works fine when I use ISE console but when I try to convert my ps1 script to exe with ps2exe, I get an error when I execute my exe file because $ScriptPath returns null value.
I tried this way but without success

$ScriptPath = Split-Path -Parent $PSCommandPath

I would like the script to be compatible with version 2.
How could I solve it?
Thank you

1 Answer 1

5

You can obtain the path with following script:

if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript")
{ 
   $ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition 
}
else
{ 
   $ScriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0]) 
   if (!$ScriptPath){ $ScriptPath = "." } 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.