Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "open with" in a batch file

I have a windows powershell script that will be available on a server to my users. I don't want them to have to go out and find the PS script, right click and click "run with powershell" or do an "open with". The Windows (Win 7 at least) default program is notepad.

I want to make a batch file to do this. I've tried:

start "c:\myfile.ps1" powershell.exe

and a few other variations, but all I've been able to do is either start powershell, or open my file in its default program, notepad.

Any advice is appreciated! Thanks!

Bonus question: If I run my batch file as administrator will it also run my PS script as administrator?

like image 699
coinbird Avatar asked Mar 08 '17 22:03

coinbird


1 Answers

Simply use the -file argument for PowerShell.exe in your batch file:

PowerShell.exe -file c:\MyFile.ps1

Additionally, some users may have their Execution Policy set to something that would restrict scripts from being executed, so you may want to do something like:

PowerShell.exe -ExecutionPolicy Bypass -file c:\MyFile.ps1

If you would like to use start to launch it you can do so as Ansgar Wiechers noted by running:

start "" PowerShell.exe -ExecutionPolicy Bypass -file c:\MyFile.ps1

Some notes regarding using start: By default it will launch PowerShell in a separate window, and continue to execute the rest of the batch file without waiting for the PowerShell window to close. If that is undesirable you have two options. You can specify /wait which will wait for the PowerShell window to close before continuing the batch file, or you can use the /B option will will not open a new window, and will execute PowerShell in the current console window.

And finally, yes if your batch file is run under the Administrator context, PowerShell will be as well.

like image 65
TheMadTechnician Avatar answered Oct 20 '22 16:10

TheMadTechnician