How can I enable script execution using PowerShell?
I’ve been working with PowerShell for over a decade, and if you’re using Windows Server 2008 R2, both the x64 and x86 versions of PowerShell need to have their execution policies set. Did you set the execution policy for both versions?
As an Administrator, you can set the execution policy by entering the following command in your PowerShell window:
Set-ExecutionPolicy RemoteSigned
For more information, see the documentation on the Set-ExecutionPolicy
cmdlet.
When you are done, you can reset the policy to its default value with:
Set-ExecutionPolicy Restricted
If you encounter an error such as:
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the “Run as administrator” option. To change the execution policy for the current user, run:
Set-ExecutionPolicy -Scope CurrentUser
So, you may need to run the command like this:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
With my extensive experience in system administration, I can add that to bypass the execution policy for a single file, you can add the -ExecutionPolicy Bypass
flag when running PowerShell:
powershell -ExecutionPolicy Bypass -File script.ps1
This command will allow you to run the script.ps1
file without being restricted by the execution policy. It’s a handy way to execute specific scripts without changing the global policy.
From my years of handling various PowerShell scenarios, another approach you might find useful is running the following command before executing the script:
Set-ExecutionPolicy Unrestricted
This command sets the execution policy to Unrestricted
, which allows scripts to run without any restrictions. However, it’s important to note that setting the execution policy to Unrestricted
can pose security risks, so it should be used with caution. Always revert to a more secure setting when done, like this:
Set-ExecutionPolicy Restricted