How do I run a PowerShell script even if Set-ExecutionPolicy is banned?
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>
Oisin Grehan has an interesting post on his blog which provides another way to bypass the execution policy. Open a shell and run this:
function Disable-ExecutionPolicy {
($ctx = $executioncontext.gettype().getfield(
"_context", "nonpublic,instance").getvalue(
$executioncontext)).gettype().getfield(
"_authorizationManager", "nonpublic,instance").setvalue(
$ctx, (new-object System.Management.Automation.AuthorizationManager
"Microsoft.PowerShell"))
}
Disable-ExecutionPolicy
This removes the default host authorization manager which will allow you to call scripts from that shell. You'd have to run this for each shell you open though since the execution policy is only overridden in the shell in which this is run.
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.