How to "open with" in a batch file

I have a windows powershell script that will be available on the server for my users. I don’t want them to go outside and find the PS script, right-click and click “run using PowerShell” or “open with”. By default, the Windows program (Win 7 by default) is a record.

I want to make a batch file for this. I tried:

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

and several other options, but all I could do was either start powershell or open the file in my program by default, 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?

+5
source share
1 answer

Just use the -file argument to PowerShell.exe in the batch file:

 PowerShell.exe -file c:\MyFile.ps1 

In addition, some users may have their own execution policy for something that would restrict the execution of scripts, so you can do something like:

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

If you want to use start to start it, you can do this, as Ansgar Vichezer noted:

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

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

And finally, yes, if your batch file is running in the admin context, PowerShell will also be.

+6
source

Source: https://habr.com/ru/post/1265207/


All Articles