pythonwindowspowershellexecutionpolicy

Powershell Execution Policy is restricted, but only when run through python


I have a Windows Server 2008 box. I have run, as administrator, in powershell Set-ExecutionPolicy Unrestricted. When I run powershell Get-ExecutionPolicy, it returns Unrestricted.

I have created a .bat file which exhibits the issue I am seeing. The .bat file, test.bat, is only one line, powershell Get-ExecutionPolicy. When I open command prompt and run test.bat, "Unrestricted" is printed to the screen.

If I run the following python script however,

import subprocess
subprocess.Popen("test.bat")

"Restricted" is printed to the screen. How is this setting different when being run from Python's subprocess.Popen? I am not seeing the issue on a Windows 7 machine I have, where I have performed an identical workflow.


Solution

  • I found the answer. The Unrestricted ExecutionPolicy was for the 64-bit version of powershell. Python ends up calling the 32-bit version, which I had not set. Windows Server 2008 includes two versions of powershell, but Windows 7 does not.

    Please see: PowerShell says "execution of scripts is disabled on this system." for more details