Monday, April 9, 2012

Powershell Find large images

Save the below code in red to .psm1 file eg. ImageFilter.psm1 and then issue following command in powershell console: Import-Module .\ImageFilter.psm1

then you can call the GetFilteredImages function, all params are optional

eg. GetFilteredImages -path c:\temp -exportFile test.txt -width 600 -height 600


*********************************************

Add-Type -Assembly System.Drawing


Function GetFilteredImages
{
Param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[String]
$path = (Get-Location -PSProvider FileSystem).ProviderPath
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[String]
$exportFile = "$path\result.csv"
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[Int]
$width = 1000
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[Int]
$height = 500
)
Process
{
Try
{
Get-ChildItem -Path $path -Recurse -Include *.jpg, *.png, *.gif |
ForEach-Object {
$Image = [System.Drawing.Image]::FromFile($_.FullName)
New-Object -TypeName System.Management.Automation.PSObject -Property @{
Name = $_.Name
Length = $_.Length
Width = $Image.Width
Height = $Image.Height
Fullname = $_.Fullname
}| Where-Object { $_.Width -gt $width -and $_.Height -gt $height}
$Image.Dispose()
} | Select-Object Name, Height, Width, Length, FullName| Export-Csv -NoTypeInformation -Path $exportFile
}
Catch
{
write-warning "Error : $($_.Exception.Message)"
}
}
}

No comments: