Home » Questions » Computers [ Ask a new question ]

How to enable execution of PowerShell scripts?

How to enable execution of PowerShell scripts?

When I try to execute my PowerShell script I get this error:

Asked by: Guest | Views: 301
Total answers/comments: 5
bert [Entry]

"Start Windows PowerShell with the ""Run as Administrator"" option. Only members of the Administrators group on the computer can change the execution policy.
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 Scripts at Microsoft TechNet Library."
bert [Entry]

"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"
bert [Entry]

"A .reg file with:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell]
""EnableScripts""=dword:00000001 ""ExecutionPolicy""=""Bypass""

and:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell]
""EnableScripts""=dword:00000001 ""ExecutionPolicy""=""Unrestricted""

works indeed too."
"A .reg file with:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell]
""EnableScripts""=dword:00000001 ""ExecutionPolicy""=""Bypass""

and:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell]
""EnableScripts""=dword:00000001 ""ExecutionPolicy""=""Unrestricted""

works indeed too."
bert [Entry]

For some reason the PowerShell cmdlet did not enable local execution globally, just for the local user context. If I tried to start a Powershell script from inside CygWin's bash prompt, for example, which runs under its own user context, it would not run, giving the "is not digitally signed" error. The answer was to go into the Local Group Policy Editor -> Local Computer Policy -> Administrative Templates -> Windows Components -> Windows PowerShell and double-click on 'Turn on Script Execution'. This then let me change it to 'Enabled' and then execution policy of "Allow local scripts and remote signed scripts" and have it work globally regardless of user context.
bert [Entry]

"The accepted answer is right, but the policy modification is only available for the currently running instance of the Powershell, meaning once the instance of the Powershell is shut down. The policy will be reset. If a user reopens another instance of Powershell, the default policy will be applied which is Restricted

For me, I need to use the VisualStudio Code console and g++ from cygwin to build things. The console is using Powershell, with the default policy, nothing can be done. One solution is changing the policy everytime the console is fired in VisualStudio Code console, maybe a script of changing the policy.

I am lazy, so another solution is when I run the Powershell in admin mode, similar to what the accepted answer does. but with an extra parameter which changes values in the Registry table. Once it been done. Other instances of Powershell will use the RemoteSigned policy by default.

set-executionpolicy remotesigned -Scope CurrentUser"