If PowerShell won’t let you run a script, you may have to enable script execution first, for example like this:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

This won’t work when execution policy was defined at GPO level, because GPO settings always have a higher precedence:

 
PS C:\> Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy      Restricted
   UserPolicy       Undefined
      Process          Bypass
  CurrentUser          Bypass
 LocalMachine       Undefined 
 

In this case, you can replace the internal PowerShell authorization manager with a fresh one. Once you run the code below, PowerShell will always execute scripts in that particular 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'))

Twitter This Tip! ReTweet this Tip!

Anonymous