PowerShell God Mode

by May 27, 2014

Before you can run a PowerShell script, the execution policy needs to allow this. Typically, you would use this line to enable script execution:

However, if group policy has disabled script execution, then this line will not do you any good. In this case, you can re-enable script execution with this code (per PowerShell session):

$context = $executioncontext.GetType().GetField('_context','nonpublic,instance').GetValue($executioncontext)
$field = $context.GetType().GetField('_authorizationManager','nonpublic,instance')
$field.SetValue($context,(New-Object Management.Automation.AuthorizationManager 'Microsoft.PowerShell'))

Note that this is a hack, effectively resetting the authorization manager, which may or may not have other side effects. Use at your own risk.

This technique is not a security issue by the way. Execution policy generally is not a security boundary. It’s not designed to keep bad people away. It is solely meant to protect you from yourself. So whether you enable script execution via cmdlet or via this code, you are in both cases consenting to taking the responsibility of executing PowerShell code into your own hands.

Twitter This Tip! ReTweet this Tip!