How to enable execution of PowerShell scripts?
2013-11
When I try to execute my PowerShell script I get this error:
File C:\Common\Scripts\hello.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:13
+ .\hello.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
Enable running unsigned scripts by entering:
set-executionpolicy remotesigned
This will allow running unsigned scripts that you write on your local computer and signed scripts from Internet.
See also Running Windows PowerShell Scripts
The Default Execution Policy is set to restricted, you can see it by typing:
Get-ExecutionPolicy
You should type the following to make it go to unrestricted mode:
Set-ExecutionPolicy unrestricted
Hope this helps
On my machine that I use to dev scripts, I will use -unrestricted as above. When deploying my scripts however, to an end user machine, I will just call powershell with the -executionpolicy switch:
powershell.exe -noprofile -executionpolicy bypass -file .\script.ps1
Using Win7-32bit and Powershell ISE I tried to run a just saved script (let's say containing a simple cls). I got the error, that execution of scripts is forbidden with the usual "get-help about_signing" advice.
When using "set-executionpolicy remotesigned" to solve I got a dialogoue to verify whether I'm sure. But if I accept another error comes up:
acces to registry key "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" denied
+ set-executionpolicy <<<< remotesigned
+ CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
What shall I do, to run saved scripts as user (or automatable as dayly service)?
You need to run powershell as an administrator to set the execution policy.
Once you've set it to remotesigned, you can run powershell as a regular user.
Just go to the controll panel -> administrative tools -> Windows PowerShell Modules (opens a powershell) and type the usual "set-executionpolicy remotesigned". That's it.
I found the solution by adding the permissions directly to the windows registry.
Wrote about it here: Powershell 101
Hope this helps.