How to “open with” in a batch file

Question:

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:

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?

Answer:

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

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:

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

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.

Source:

How to “open with” in a batch file by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply