Powershell – Bulk check of image files
When dealing with collections of thousands of photos, it becomes necessary to use scripts to do things in an efficient manner.
This script checks each image within the specified folder for errors. It writes the result to the console and to a log file which is saved to the same specified folder location.
This script was largely produced by Cline (Anthropic) AI. It adds a header that includes a time-stamp and ends with a summary of the results.
# Load System.Drawing assembly for image processing
Add-Type -AssemblyName System.Drawing
function Test-ImageFile {
param (
[Parameter(Mandatory=$true)]
[string]$ImagePath
)
try {
$image = [System.Drawing.Image]::FromFile($ImagePath)
$image.Dispose()
return $true
}
catch {
return $false
}
}
function Check-ImagesInFolder {
param (
[Parameter(Mandatory=$true)]
[string]$FolderPath
)
# Create log file in the target folder
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$logFile = Join-Path $FolderPath "ImageCheck_$timestamp.log"
$header = @"
Image Check Report
Timestamp: $(Get-Date)
Target Folder: $FolderPath
----------------------------------------
"@
$header | Out-File -FilePath $logFile
Write-Host "Checking images in folder: $FolderPath" -ForegroundColor Cyan
Write-Host "----------------------------------------" -ForegroundColor Cyan
# Function to write both to console and log file
function Write-OutputAndLog {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
Add-Content -Path $logFile -Value $Message
}
# Supported image formats
$imageExtensions = @("*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp")
$errorCount = 0
$totalCount = 0
foreach ($ext in $imageExtensions) {
$images = Get-ChildItem -Path $FolderPath -Filter $ext -Recurse -File
foreach ($image in $images) {
$totalCount++
$status = "Checking: $($image.Name)..."
Write-Host $status -NoNewline
Add-Content -Path $logFile -Value $status -NoNewline
if (Test-ImageFile -ImagePath $image.FullName) {
Write-Host " OK" -ForegroundColor Green
Add-Content -Path $logFile -Value " OK"
}
else {
Write-Host " ERROR" -ForegroundColor Red
Write-Host " - Path: $($image.FullName)" -ForegroundColor Yellow
Add-Content -Path $logFile -Value " ERROR"
Add-Content -Path $logFile -Value " - Path: $($image.FullName)"
$errorCount++
}
}
}
$summary = @"
Scan Complete
----------------------------------------
Total images scanned: $totalCount
Images with errors: $errorCount
Status: $(if ($errorCount -gt 0) { "Issues Found" } else { "All Images OK" })
"@
Write-OutputAndLog "`nScan Complete" "Cyan"
Write-OutputAndLog "----------------------------------------" "Cyan"
Write-OutputAndLog "Total images scanned: $totalCount"
Write-OutputAndLog "Images with errors: $errorCount"
Write-OutputAndLog "Status: $(if ($errorCount -gt 0) { 'Issues Found' } else { 'All Images OK' })" $(if ($errorCount -gt 0) { "Red" } else { "Green" })
Write-OutputAndLog "Log file created at: $logFile" "Cyan"
}
# Example usage:
# Check-ImagesInFolder -FolderPath "C:\Path\To\Your\Images"