If you are like me, you hate having to type in the full path to a PowerShell script to run it.Wouldn’t it be nice if you could just execute it? I’m an old Windows scripter guy (DOS, VBScript, etc), and I’m used to being able to just double-click a batch or script file to run it. It’s part of the reason I always prompt for arguments in my PowerShell scripts if they are not supplied. I expect to be able to double-click a script, have it ask for what it needs, and then do whatever it’s supposed to do.
Well, one option is obvious. I could map the “Open With” for PowerShell scripts to powershell.exe, but then the script window will close as soon as it completes, and I won’t have a change to read any errors or output from the script. That’s a problem. I resorted a trick I used to use for Remote Desktop configuration files from back before there was a Remote Desktop Manager. I used to save the configuxdrration files for commonly used servers to a folder. then I wrote a batch file that made the appropriate call to the RDP executable passing in the configuration file as a command line argument. In Windows scripting, there are built-in positional parameters for arguments. Rather than naming arguments, I can refer to them as %1% for the first argument, %2% for the second argument, and so on up to %9%.
By using %1% as the argument to pass in to the RDP executable, I could simply drag-and-drop the configuration file, and it passed that in to the executable as an argument. Works perfectly well with powershell.exe and PowerShell scripts too. Then I add a PAUSE statement to have it wait for me to hit a key before closing the window.
This is the contents of the batch file I created:
From this point, I can run a script by dragging and dropping it on the batch file or I can configure it as the default “Open With” program for all PowerShell files. Me, I prefer to just use drag-and-drop so I don’t accidentally run any scripts I didn’t mean to run.