pest_control Sympthoms
- When you try to execute a PowerShell script, you get the following error message:
C:\Users\username\Desktop\script.ps1 : File C:\Users\username\Desktop\script.ps1 cannot be loaded because running
scripts is disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ C:\Users\username\Desktop\script.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
search Causes
- The ability to run PowerShell scripts on the system is influenced by Execution Policies. You can refer to the official Microsoft documentation to learn more about it (about Execution Policies).
- The default execution policy for the “LocalMachine” scope is “Restricted“
construction Fix/Workaround/Solution
Execution policies are an important security feature on Windows, they can be applied to different scopes (as the official documentation says: “On a Windows computer you can set an execution policy for the local computer, for the current user, or for a particular session.”) and can be set up via group policies when the computer/server is domain joined.
The aim of this guide is SOLELY to make you able to quickly run a PowerShell script on your system, remember that setting up proper execution policies is mandatory to keep your OS safe.
With that being said, here are the steps to follow:
- (OPTIONAL) Open a PowerShell and run the following command to get a list of the system’s current execution policies:
- Open an elevated PowerShell (run as Administrator) and run the following command:
- Press Y when prompted
Get-ExecutionPolicy -List
Set-ExecutionPolicy Unrestricted
Notice how we are not specifying to which scope we want to apply the “Unrestricted” policy to. That’s because if not otherwise specified the default scope is “LocalMachine“.
You can now execute the script. If you still encounter problems, the problem lies somewhere else and you will need to analyze the error message to investigate the issue further.
school Further considerations
As stated before, messing around with execution policies is not recommended if you don’t have a general idea of what you’re doing, so apply this quick workaround only for the time necessary to run your scripts.
Here's a list of official Microsoft documentation on the topic that you might find useful:
Article ID: SYS-WIN-0003