When working with PowerShell scripts, you may occasionally run into an error saying “ps1 cannot be loaded because running scripts is disabled on this system”. This typically occurs when trying to run a
.ps1 file from the command line or within another application like Visual Studio Code.
The root cause is that by default, PowerShell has execution policies configured to restrict running of scripts for security purposes. To execute scripts, the execution policy needs to be changed.
There are a few different options for resolving this:
The simplest and most secure approach is to run:
This will allow running local and downloaded scripts signed by trusted publishers. It provides a good balance of security while allowing common dev workflows.
For a quick fix within the current PowerShell session only, use:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
This bypasses all security checks but only lasts for the current process.
As a last resort, you can set the policy to unrestricted:
But this opens up your system to all script execution and is not recommended for production environments.
Understanding Execution Policy Scope for Running Scripts
The execution policy can also be configured differently based on scope like the current user or local machine. Make sure to check the scope if an issue persists after changing the policy.
With the right execution policy set, you should then be able to successfully run your
.ps1 scripts without seeing the error message. Let me know if you have any other questions!
You can always reach out to me on Twitter.
Reference: Pinal Dave (https://blog.sqlauthority.com)