PowerShell Cmdlet: Unblock-File
Welcome back to Wahmans PowerShell Blog! Today, we explore a handy cmdlet that addresses a common frustration when executing downloaded files — Unblock-File.
What is Unblock-File?
According to Microsoft, Unblock-File “unblocks files that were downloaded from the internet.” Windows attaches a security flag (Zone.Identifier alternate data stream) to files that have been downloaded from the internet, and PowerShell prevents running certain scripts with this flag present. The Unblock-File cmdlet removes this block so they’re ready for execution.
Basic Syntax
Unblock-File -Path "path-to-your-file"
Use Cases and Examples
1. Beginner: Unblock a Single Script File
You downloaded a PowerShell script and want to execute it without being blocked:
Unblock-File -Path "C:\Users\YourUser\Downloads\script.ps1"
2. Intermediate: Unblock All Scripts in a Folder
Unblock multiple scripts in one go. For example, all .ps1 files in a download folder:
Get-ChildItem -Path "C:\Users\YourUser\Downloads" -Filter "*.ps1" | Unblock-File
3. Advanced: Unblock Files Based on Alternate Data Stream Check
You might want to check whether files are blocked and only unblock those that are. This ensures performance and avoids unnecessary operations:
Get-ChildItem "C:\Downloads" -Recurse | Where-Object {
($_ | Get-Item).AlternateDataStream -match 'Zone.Identifier'
} | Unblock-File
4. Power-User: Unblock Downloaded Modules Automatically
If you download modules manually and place them in your module path, use this snippet to unblock all files within a module:
$ModulePath = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\MyModule"
Get-ChildItem -Path $ModulePath -Recurse | Unblock-File
Wrap-Up
Unblock-File is an essential tool in any PowerShell user’s kit — especially when working with files sourced from the web. It promotes safe, smooth script execution without needing to disable security features altogether.
Happy scripting, and I will see you in the next post!
Leave a Reply