Home > OS >  Is there a way to start a file with my PowerShell compiled script per double click?
Is there a way to start a file with my PowerShell compiled script per double click?

Time:11-27

I have created a PowerShell script and then compiled it with PS2EXE. The program loads *.JSON-files and allows the user to edit it. The program starts with a file browser window to select the right *.JSON to load.

Now I want to try to run the program by double-click a *.JSON and skip the file browser window. So I right klick the file > select open with > and choose mypowershellapp.exe and inspected the $PWD var. But it was always the system32 directory.

So: How to get the file path the program was started from?

CodePudding user response:

Indeed: when you use File Explorer's Open with... shortcut menu and select an application to pass the document at hand to, that application invariably launches with C:\Windows\SYSTEM32 as its working directory.

However, the document at hand is passed as an argument by its full path, so you can infer the directory it is located in.

That is, in your (compiled) *.ps1 script, do something like the following:

# Note: 
#  * Use of $args[0] assumes that your script declares no formal parameters.
#  * Adjust as needed.
# $args[0] is the document's full *file* path.
# Split-Path -Parent extracts the *directory* path from it.
$dir = Split-Path -Parent $args[0]

Of course, you can also pass this directory to Set-Location to change the application's working directory.

  • Related