Search code examples
powershellworking-directory

Get real exe location path


I am trying to execute 2 exe (File1.exe and File2.exe) via another script converted to exe (Run.exe) .. it works fine when I use the lines in the Run.exe script:

Start-Process -FilePath "$(Get-Location)\Folder1\File1.exe"
Start-Process -FilePath "$(Get-Location)\Folder2\File2.exe" 

The problem is that File1.exe and File2.exe get the location of the launcher (which is Run.exe) instead of their own:

My schema is:

Desktop\run.exe
Desktop\folder1\file1.exe
Desktop\folder2\file2.exe

script in File1.exe and file2.exe is:

write-host "$(Get-Location)"

So this must print C:\Users\Sensei\Desktop\folder1 and C:\Users\Sensei\Desktop\folder2 in the console windows of file1.exe and file2.exe when I run the launcher (Run.exe), but what I get is the launcher path, which is C:\Users\Sensei\Desktop


Solution

  • The answer is here :

    $FullPathToEXE = [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName
    
    $DirectoryContainingEXE = [System.IO.Path]::GetDirectoryName($FullPathToEXE)