Skip to Content

Recursively Unblock Files Powershell File

foreach ($file in $files) $processed++ if ($ShowProgress) $percent = [math]::Round(($processed / $total) * 100, 1) Write-Progress -Activity "Unblocking Files" -Status "Processing $($file.Name)" -PercentComplete $percent -CurrentOperation "$processed of $total" try $hasZoneId = Get-Item $file.FullName -Stream Zone.Identifier -ErrorAction SilentlyContinue if ($hasZoneId) if ($PSCmdlet.ShouldProcess($file.Name, "Unblock")) Unblock-File -Path $file.FullName -ErrorAction Stop $unblocked++ $status = "UNBLOCKED" Write-Host "[UNBLOCKED] $($file.FullName)" -ForegroundColor Green else $status = "SKIPPED (WhatIf)" $results += [PSCustomObject]@ File = $file.FullName Status = $status Timestamp = Get-Date "$status`: $($file.FullName)" catch $errorMsg = "FAILED: $($file.FullName) - $_" Write-Warning $errorMsg $errorMsg

Write-Host "Scanning: $targetPath" -ForegroundColor Cyan recursively unblock files powershell

# Get all files recursively $files = Get-ChildItem -Path $targetPath -File -Recurse -ErrorAction SilentlyContinue recursively unblock files powershell

Write-Progress -Activity "Unblocking Files" -Completed recursively unblock files powershell

foreach ($file in $files) try # Check if file has zone identifier (downloaded from internet) $stream = $file.GetAccessControl() $hasZoneId = (Get-Item $file.FullName -Stream * -ErrorAction SilentlyContinue catch Write-Warning "Failed to unblock: $($file.FullName) - $_"

unblock-all "C:\Downloads" This feature is useful for safely handling downloaded files that Windows marks with an alternate data stream (Zone.Identifier) to indicate they came from the internet.

return $results # Basic usage - unblock everything in current folder and subfolders Unblock-FilesRecursively Specify a different path Unblock-FilesRecursively -Path "C:\Downloads" Only unblock PowerShell scripts and executables Unblock-FilesRecursively -Path "D:\Projects" -IncludeExtensions @("ps1", "exe") Preview what would be unblocked without actually doing it Unblock-FilesRecursively -WhatIf Advanced version with logging and progress Invoke-RecursiveUnblock -Path "C:\Users$env:USERNAME\Downloads" -Filter "Executables" -ShowProgress With confirmation prompt Invoke-RecursiveUnblock -Path "." -WhatIf Quick Alias for Frequent Use Add to your PowerShell profile: