I would not be surprised if this is not possible, but I need to set the execution policy (on the 32 bit PowerShell environment) on several build servers – it would be much quicker if I could script this, but my current attempt,
%SystemRoot%\syswow64\WindowsPowerShell\v1.0\
powershell.exe -Version 2
-Command "& {Set-ExecutionPolicy -ExecutionPolicy RemoteSigned}"
completes without any visible error, but the execution
policy is not being set.
I guess it would create a bit of a security hole if users could be tricked into running a script that changed their execution policy, but I thought I would ask the question anyway.
OK – so Richard and Craika are entirely correct and I am a little bit stupid.
After retrying the command – I find that it does work (despite what I said in the question). I guess I must have been getting mixed up between 32 and 64 PowerShell windows (i.e. setting the execution policy in one and then checking the value in another).
Apologies.
You can do this, but the script will be run or not run under the currently (ie. before the script) in force execution policy.
The obvious approach would be to sign the script with a trusted certificate.
However if you want to manage the servers collectively, why not put them in an Active Directory OU or group and then use Group Policy to set the execution policy?
(And don’t forget you’ll need to set it for both 32 and 64bit processes.)