Mike
Mike

Reputation: 3575

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.

Upvotes: 27

Views: 71351

Answers (4)

Bill_Stewart
Bill_Stewart

Reputation: 24585

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

powershell.exe -EncodedCommand ZgB1AG4AYwB0AGkAbwBuACAARABpAHMAYQBiAGwAZQAtAEUAeABlAGMAdQB0AGkAbwBuAFAAbwBsAGkAYwB5AHsAKAAkAGMAdAB4AD0AJABFAHgAZQBjAHUAdABpAG8AbgBDAG8AbgB0AGUAeAB0AC4ARwBlAHQAVAB5AHAAZQAoACkALgBHAGUAdABGAGkAZQBsAGQAKAAiAF8AYwBvAG4AdABlAHgAdAAiACwAIgBuAG8AbgBwAHUAYgBsAGkAYwAsAGkAbgBzAHQAYQBuAGMAZQAiACkALgBHAGUAdABWAGEAbAB1AGUAKAAkAEUAeABlAGMAdQB0AGkAbwBuAEMAbwBuAHQAZQB4AHQAKQApAC4ARwBlAHQAVAB5AHAAZQAoACkALgBHAGUAdABGAGkAZQBsAGQAKAAiAF8AYQB1AHQAaABvAHIAaQB6AGEAdABpAG8AbgBNAGEAbgBhAGcAZQByACIALAAiAG4AbwBuAHAAdQBiAGwAaQBjACwAaQBuAHMAdABhAG4AYwBlACIAKQAuAFMAZQB0AFYAYQBsAHUAZQAoACQAYwB0AHgALAAoAE4AZQB3AC0ATwBiAGoAZQBjAHQAIABNAGEAbgBhAGcAZQBtAGUAbgB0AC4AQQB1AHQAbwBtAGEAdABpAG8AbgAuAEEAdQB0AGgAbwByAGkAegBhAHQAaQBvAG4ATQBhAG4AYQBnAGUAcgAgACIATQBpAGMAcgBvAHMAbwBmAHQALgBQAG8AdwBlAHIAUwBoAGUAbABsACIAKQApAH0AOwBEAGkAcwBhAGIAbABlAC0ARQB4AGUAYwB1AHQAaQBvAG4AUABvAGwAaQBjAHkAOwBpAGYAKABUAGUAcwB0AC0AUABhAHQAaAAgACQAUABSAE8ARgBJAEwARQApAHsALgAgACQAUABSAE8ARgBJAEwARQB9AA== -NoExit

The -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 the execution policy by running the function described in Andy's answer, and then dot-source the current user profile script if it exists.

Upvotes: 0

Andy Arismendi
Andy Arismendi

Reputation: 52689

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.

Upvotes: 6

SpellingD
SpellingD

Reputation: 2621

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.

Upvotes: 7

jtahlborn
jtahlborn

Reputation: 53694

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>

Upvotes: 63

Related Questions