[ACCEPTED]-How do I run a PowerShell script even if Set-ExecutionPolicy is banned?-powershell

Accepted answer
Score: 63

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

powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File <script_name>
Score: 7

The easiest silliest way around this is just:

gc .\script.ps1 | iex

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

Score: 6

Oisin Grehan has an interesting post on his blog which provides another way to bypass the 7 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 6 removes the default host authorization manager 5 which will allow you to call scripts from 4 that shell. You'd have to run this for each 3 shell you open though since the execution 2 policy is only overridden in the shell in 1 which this is run.

Score: 0

This is a slight enhancement to Andy's answer 11 to this question. If an admin sets a restrictive 10 PowerShell script execution policy in a 9 GPO (probably under the mistaken notion 8 that PowerShell execution policy is a security 7 boundary), you can get a PowerShell interactive 6 session that has execution policy disabled 5 by using the following command line:

powershell.exe -EncodedCommand ZgB1AG4AYwB0AGkAbwBuACAARABpAHMAYQBiAGwAZQAtAEUAeABlAGMAdQB0AGkAbwBuAFAAbwBsAGkAYwB5AHsAKAAkAGMAdAB4AD0AJABFAHgAZQBjAHUAdABpAG8AbgBDAG8AbgB0AGUAeAB0AC4ARwBlAHQAVAB5AHAAZQAoACkALgBHAGUAdABGAGkAZQBsAGQAKAAiAF8AYwBvAG4AdABlAHgAdAAiACwAIgBuAG8AbgBwAHUAYgBsAGkAYwAsAGkAbgBzAHQAYQBuAGMAZQAiACkALgBHAGUAdABWAGEAbAB1AGUAKAAkAEUAeABlAGMAdQB0AGkAbwBuAEMAbwBuAHQAZQB4AHQAKQApAC4ARwBlAHQAVAB5AHAAZQAoACkALgBHAGUAdABGAGkAZQBsAGQAKAAiAF8AYQB1AHQAaABvAHIAaQB6AGEAdABpAG8AbgBNAGEAbgBhAGcAZQByACIALAAiAG4AbwBuAHAAdQBiAGwAaQBjACwAaQBuAHMAdABhAG4AYwBlACIAKQAuAFMAZQB0AFYAYQBsAHUAZQAoACQAYwB0AHgALAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABNAGEAbgBhAGcAZQBtAGUAbgB0AC4AQQB1AHQAbwBtAGEAdABpAG8AbgAuAEEAdQB0AGgAbwByAGkAegBhAHQAaQBvAG4ATQBhAG4AYQBnAGUAcgAgACIATQBpAGMAcgBvAHMAbwBmAHQALgBQAG8AdwBlAHIAUwBoAGUAbABsACIAKQApAH0AOwBEAGkAcwBhAGIAbABlAC0ARQB4AGUAYwB1AHQAaQBvAG4AUABvAGwAaQBjAHkAOwBpAGYAKABUAGUAcwB0AC0AUABhAHQAaAAgACQAUABSAE8ARgBJAEwARQApAHsALgAgACQAUABSAE8ARgBJAEwARQB9AA== -NoExit

The 4 -EncodedCommand here is the following code:

function Disable-ExecutionPolicy{($ctx=$ExecutionContext.GetType().GetField("_context","nonpublic,instance").GetValue($ExecutionContext)).GetType().GetField("_authorizationManager","nonpublic,instance").SetValue($ctx,(New-Object Management.Automation.AuthorizationManager "Microsoft.PowerShell"))};Disable-ExecutionPolicy;if(Test-Path $PROFILE){. $PROFILE}

That is, disable 3 the execution policy by running the function 2 described in Andy's answer, and then dot-source 1 the current user profile script if it exists.

More Related questions