Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a PowerShell script even if Set-ExecutionPolicy is banned?

The Set-ExecutionPolicy command of PowerShell is banned, so I can NOT run like this:

PS> .\script.ps1 (enter) 

Is there another way to run the PowerShell script except from the "Windows PowerShell ISE"?

PS: I was able to use Java's ProccessBuilder to run a single PowerShell command, but don't know how to run the whole script.

like image 611
Mike Avatar asked Feb 14 '12 04:02

Mike


People also ask

Can you execute the PowerShell script if the PowerShell execution policy is restricted?

Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode. AllSigned - Only scripts signed by a trusted publisher can be run. RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.


2 Answers

This is what we use to run PowerShell scripts from Java (works regardless of the execution policy):

powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File <script_name> 
like image 121
jtahlborn Avatar answered Sep 19 '22 21:09

jtahlborn


The easiest silliest way around this is just:

gc .\script.ps1 | iex 

This works in PowerShell and doesn't care about ExecutionPolicy. Just make sure that you are careful with newlines. Keep {}s and similar on the same line, using ;s where needed.

like image 24
SpellingD Avatar answered Sep 18 '22 21:09

SpellingD