Unblock-File – Unlocking Downloaded Files with PowerShell
Welcome back to Wahmans PowerShell blog! Today, we’re diving into a useful but sometimes overlooked PowerShell cmdlet: Unblock-File. If you’ve ever downloaded a script or file from the internet and found it blocked by Windows, this cmdlet is your key to fixing the issue.
From the PowerShell documentation:
Unblock-File
Unblocks files that were downloaded from the internet.
When you download files like PowerShell scripts (.ps1), zip archives, or executables, Windows often marks them with a security tag indicating they’re from the internet. This can prevent execution or even trigger security warnings. Unblock-File is your workaround.
Let’s Explore Some Practical Examples
1. Beginner: Unblock a Single Script
If you’ve downloaded a PowerShell script called install-module.ps1 and want to run it without security warnings, use:
Unblock-File -Path "C:\Downloads\install-module.ps1"
2. Intermediate: Unblock Multiple Files in a Directory
Say you’ve downloaded multiple files into a folder. You can unblock all of them with:
Get-ChildItem -Path "C:\Downloads\PowerTools" | Unblock-File
This scans all files in the directory and unblocks them one by one.
3. Advanced: Unblock Only PowerShell Scripts in a Folder
If you only want to unblock .ps1 files from a directory, you can filter them like this:
Get-ChildItem -Path "C:\Scripts" -Filter "*.ps1" | Unblock-File
4. Power-User: Unblock All Downloaded Files from a Zip Archive
Imagine you’ve extracted files from a zip archive that was downloaded from the internet. You can use this to remove the zone identifier attribute (blocking tag) recursively:
Get-ChildItem -Path "C:\Tools\Downloaded" -Recurse | Unblock-File
This will traverse the entire directory tree and unblock all files.
Conclusion
Unblock-File is a small utility with big usefulness when dealing with blocked files. Always be cautious when unblocking and executing scripts—make sure you trust the source before proceeding.
Happy scripting, and I will see you in the next post!
Leave a Reply