Unblock downloaded PowerShell scripts
July 17, 2017 Leave a comment
CC 2.0 image courtesy sima dimitric on Flickr
When you download and try to run a PowerShell script (a .ps1 file extension) from the internet, you see the following security warning:
Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer. If you trust this script, use the Unblock-File cmdlet to allow the script to run without this warning message. Do you want to run {script name}?
Cause
Windows prevents downloaded internet files from being runnable as part of built-in Windows security. This impedes malicious scripts from running on your machine. This error message will even appear when your PowerShell ExecutionPolicy is set to Unrestricted. To adjust your execution policy settings, see PowerShell Execution Policy Settings.
Resolution
The temporary solution to this problem is to click the Run once button each time you want to run the script. However, if you need to run this script often this warning message can be annoying. There are three permanent solutions to fix the issue.
Option 1: Bypass ExecutionPolicy
As mentioned above, an Unrestricted execution policy does not prevent the error message from appearing. To disable all warning messages permanently for any downloaded internet files, you need to use the Bypass ExecutionPolicy. However, this command unblocks every script you download, trustworthy or not.
To execute this command, open a PowerShell window with administrator privileges and run the following:
Set-ExecutionPolicy Bypass
You can also reset the ExecutionPolicy back to its default setting of Restricted using
Set-ExecutionPolicy Restricted
Option 2: Unblock-File cmdlet
Instead of changing permissions for every script in PowerShell (Option 1), you can instead unblock only the file needed. To do this, run the Unblock-File cmdlet recommended in the warning message above. To unblock the file, use the following command:
Unblock-File script_name.ps1
NOTE: You do not need elevated permissions to run this command.
Option 3: File Properties
The final option is to change the properties of the file itself. Since the file was downloaded, Windows allows you to change the file’s properties.To unblock the file:
- Browse to the file using Windows Explorer.
- Right-click the file and select Properties.
- On the General tab, under Security, click the Unblock checkbox.
- Click OK.
Conclusion
This article has shown you three ways of running blocked PowerShell scripts downloaded from the internet. It’s recommended you grant the least permissions possible (Option 2 or Option 3) before making sweeping changes (Option 1).