One of the handy cmdlets available in PowerShell is Unblock-File
– essential for dealing with files downloaded from the Internet that Windows has marked as unsafe. This PowerShell tutorial will discuss how to use the PowerShell unblock-file cmdlet to unblock files.
What is the PowerShell’s Unblock-File Cmdlet?
When you download files from the Internet, Windows may block them as a security measure to prevent potentially harmful scripts from running on your system. These files have an alternate data stream that tags them as being from an external source. Unblock-File
is a cmdlet that can remove this block, allowing you to use the files normally.
How to use Unblock-File cmdlet in PowerShell
To use Unblock-File
, you’ll need to open PowerShell with administrative privileges. You can do this by searching for PowerShell in the Start menu, right-clicking on it, and selecting “Run as administrator.”
Basic Syntax
The basic syntax of the PowerShell Unblock-File
 cmdlet is as follows:
Unblock-File -Path <Path-to-the-File>
Replace <Path-to-the-File>
with the actual path to the file you want to unblock.
Example
Imagine you’ve downloaded a script called setup.ps1
 And you try to run it, only to encounter a warning that it has been blocked. To unblock this file, you would use the following command:
Unblock-File -Path C:\Downloads\setup.ps1
This command will remove the block from the file, allowing you to execute the script without any warnings.
Confirming Unblock Actions
If you want to be prompted for confirmation before each file is unblocked, you can use the -Confirm
switch:
Unblock-File -Path C:\Downloads\*.ps1 -Confirm
Every time a file is about to be unblocked, PowerShell will ask for your confirmation.
Using the WhatIf Parameter
If you’re not sure what files will be affected by the Unblock-File
cmdlet, you can append the -WhatIf
parameter to see what actions will be taken without actually unblocking the files:
Unblock-File -Path C:\Downloads\*.ps1 -WhatIf
Unblock Multiple Files in PowerShell using Unblock-File cmdlet
You can also use wildcards to unblock multiple files at once in PowerShell. For instance, if you want to unblock all .ps1
files in a directory, you could use:
Unblock-File -Path C:\Downloads\*.ps1
This will unblock all files with the .ps1
extension in the Downloads folder.
Here is another complete PowerShell script to unblock multiple files using unblock-file cmdlet.
# PowerShell Script to Unblock Multiple Files
# Specify the directory path where the downloaded files are located
$directoryPath = "C:\Files"
# Retrieve all files in the directory
$files = Get-ChildItem -Path $directoryPath
# Iterate over each file and unblock it
foreach ($file in $files) {
# Check if the file is blocked
if ((Get-ItemProperty -Path $file.FullName -Name ZoneIdentifier -ErrorAction SilentlyContinue) -ne $null) {
# Unblock the file
Unblock-File -Path $file.FullName
Write-Host "Unblocked: $($file.Name)"
}
else {
Write-Host "Already unblocked: $($file.Name)"
}
}
Write-Host "All files have been processed."
Here is the script explanation:
- The script starts by setting a variable
$directoryPath
to the path where your downloaded files are located. Get-ChildItem
cmdlet is used to retrieve all the files in the specified directory.- A
foreach
loop iterates over each file, and for each one, it checks if aZoneIdentifier
stream exists, which indicates that the file is blocked. - If the file is blocked,
Unblock-File
is used to unblock it, and a message is displayed in the console. - If the file is not blocked, a message indicating that it is already unblocked is displayed.
Conclusion
The Unblock-File
 cmdlet is a very useful command in PowerShell that helps you manage the security of files you download from the Internet. With the Unblock-File
 cmdlet, you have a quick and efficient way to clear the “blocked” status from files in PowerShell. I have also explained how to use the PowerShell unblock-file cmdlet to unblock multiple files.
You may also like:
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com