How to use Unblock-File cmdlet in PowerShell
Basic Syntax
Unblock-File -Path <Path-to-the-File>Example
Unblock-File -Path C:\Downloads\setup.ps1Confirming Unblock Actions
Unblock-File -Path C:\Downloads\*.ps1 -ConfirmUsing the WhatIf Parameter
Unblock-File -Path C:\Downloads\*.ps1 -WhatIfUnblock Multiple Files in PowerShell using Unblock-File cmdlet
Unblock-File -Path C:\Downloads\*.ps1# 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
$directoryPathto the path where your downloaded files are located. Get-ChildItemcmdlet is used to retrieve all the files in the specified directory.- A
foreachloop iterates over each file, and for each one, it checks if aZoneIdentifierstream exists, which indicates that the file is blocked. - If the file is blocked,
Unblock-Fileis 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
You may also like:




