PowerShell scripts are powerful tools for automating tasks, managing systems, and handling repetitive jobs effortlessly. They allow you to streamline workflows and save time. However, Windows blocks PowerShell scripts by default to protect your system from potentially harmful files.
For instance, you might encounter an error like this when trying to run a script:
File C:\Common\Scripts\hello.ps1 cannot be loaded because the execution of scripts is disabled on this system.
This error occurs because script execution is disabled by default in PowerShell. To resolve this, you need to enable script execution on your system.
This guide will walk you through the process of enabling PowerShell scripts.
Why Script Execution Is Disabled
A PowerShell script is a file that contains a series of PowerShell commands. These scripts usually have the .ps1
extension. Instead of typing each command one by one, you can run the script to execute them all at once.
Windows disables PowerShell scripts to prevent malicious files from running on your computer. This ensures that only trusted scripts are executed. If you trust the script you want to run, you can change the execution policy to allow it.
What is an Execution Policy?
An execution policy in PowerShell is a security measure that controls how scripts are run. It doesn’t prevent all scripts from running, but it restricts untrusted or unsigned scripts. There are different levels of execution policies:
-
Restricted: No scripts can run. This is the default.
-
AllSigned: Only scripts signed by a trusted publisher can run.
-
RemoteSigned: Scripts created locally can run, but downloaded scripts need to be signed.
-
Unrestricted: All scripts can run, but you will be warned before running scripts from the internet.
Checking Your Current Execution Policy
Before changing anything, you can check the current execution policy by running:
Get-ExecutionPolicy
This will return the policy currently applied to your system.
Changing the Execution Policy
To enable running PowerShell scripts, follow these steps:
Open PowerShell as Administrator: Search for PowerShell in the Start menu. Right-click on it and select Run as Administrator.
Set the Execution Policy: Use the following command to set the execution policy. Replace PolicyName
with the policy you want, such as RemoteSigned
, Unrestricted
, or Bypass
:
Set-ExecutionPolicy PolicyName
For example, to set it to RemoteSigned, type:
Set-ExecutionPolicy RemoteSigned
PowerShell will ask you for confirmation. Type A
(for "Yes to All") and press Enter.
After making the change, confirm the new policy with this command:
Get-ExecutionPolicy
It should now display RemoteSigned or the policy you chose.
Why Choose RemoteSigned?
This policy is a balanced choice. It allows locally created scripts to run but requires internet-downloaded scripts to have a trusted signature.
Temporary Changes (For One Session Only)
If you want to allow PowerShell scripts to run for the current session without making permanent changes, you can use the -Scope Process
option:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned
This setting will reset once you close PowerShell.
Running a PowerShell Script
Once the execution policy is set, you can run scripts. Navigate to the folder where your script is saved. Use the cd
command to change directories. For example:
cd C:\Scripts
Run the script by typing its name:
.\scriptname.ps1
If your script is from the internet and not signed, you may see a warning. Ensure you trust the source before proceeding.
How to Reset the Execution Policy
If you want to revert the changes and block scripts again, use this command:
Set-ExecutionPolicy Restricted
This will restore the default setting.
Troubleshooting
If you still encounter issues, try these solutions:
-
Group Policy Settings: Some systems have group policies that override PowerShell’s execution policy. Check with your system administrator.
-
Use the Unblock-File Cmdlet: If the script was downloaded, run:
Unblock-File -Path "C:\Path\To\Your\Script.ps1"
-
Registry Settings: Advanced users can adjust execution policies via the registry. This is not recommended unless you know what you’re doing.
Conclusion
Changing the execution policy in PowerShell is simple and allows you to run your scripts safely. Always be cautious when enabling script execution, especially on shared or production systems.