user4132336
user4132336

Reputation: 1

Powershell: Delete images of certain dimensions

I want to delete all *.jpg files in a specific folder and all its subfolders which have e.g. width unequal 800 and height unequal 600 (leaving only 800x600 jpg images).

Can someone tell me how to do this in Powershell?

I know I can remove *.jpg images with

Get-ChildItem -Path .\ -Filter *.jpg -Recurse | foreach ($_) {remove-item $_.fullname}

But I can't seem to find how to select height/width of an image.

Upvotes: 0

Views: 2548

Answers (1)

SomeShinyObject
SomeShinyObject

Reputation: 7801

You could use the System.Drawing.Image .NET Object:

$(Get-ChildItem -Filter *.jpg).FullName | ForEach-Object { 
    $img = [Drawing.Image]::FromFile($_); 
    $dimensions = "$($img.Width) x $($img.Height)"

    If ($dimensions -eq "800 x 600") {
        Remove-Item $_
    }
}

Upvotes: 4

Related Questions