I came across a situation the other day that on a Windows Server 2008 R2 box, when I created a Scheduled Task to run a Powershell script, it runs OK using “C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe” (64 bit PowerShell) but fails with error code (0x1) if I use “C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell.exe” (32 bit Powershell)
I have done the following steps to help me troubleshoot the issue.
1. I have changed the scheduled task to “Run only when user is logged on” so a command prompt was shown when the task runs. I screen captured the output:
It looks like the PowerShell Execution Policy is preventing the script to run.
2. I confirmed the execution policy setting is set to RemoteSigned – which is sufficient for the script to run.
3. However, when I launched “Windows PowerShell (x86)”, I got different setting!
4. I didn’t know that we can have different Execution Policy for 64 Bit and 32 Bit PowerShell:
5. So to fix the problem was fairly easy – Set Execution Policy to RemoteSigned on a 32 bit PowerShell console.
As we know that the PowerShell Execution Policy setting is stored in the registry HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Micorosft.Powershell\ExecutionPolicy
The 32 bit setting is stored in another location: HKLM\SOFTWARE\Wow6432Node\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
If the value “ExecutionPolicy” is missing, the default setting is “Restricted”. once I set the it to RemoteSigned, the ExecutionPolicy is created and set to RemoteSigned. If I delete it again, the execution policy is then changed back to “Restricted”
I don’t know what is the idea behind having 2 separate execution policies for 64 bit and 32 bit Powershell, maybe someone can share more information with us…