Skip to content

Script to list image files by size and resolution, export to Excel

G24th September 2014

C8 Comments

TExcel, PowerShell, Windows

Instructions for using PowerShell to export a list of images and their properties to Excel as a tab-delimited CSV.

Windows PowerShell is a DOS-like task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. It looks a lot like Windows DOS and can work similarly. As one of its many functions, PowerShell can be used to collect information about the files on a computer and export that information to Excel as a tab-delimited comma-separated values file (CSV).

Previously, I used PowerShell to list all of the files in a directory and export their name, date of last modification and size (in bytes) to Excel.

Now, I want to create a list of images which includes their height and width in pixels, their date of creation, their size in megabytes as well as their resolution in megapixels.

Listing images of one type

Let’s assume that our images are located in c:\images. We can use the following script to locate all images in c:\images with a width and height greater than 500 pixels. The output will be placed in a file located at c:\images\img.csv.

Get-ChildItem -Recurse c:\images -Filter *.jpg | % {
    $image = [System.Drawing.Image]::FromFile($_.FullName)
    if ($image.width -gt 500 -and $image.height -gt 500) {
        New-Object PSObject -Property @{
		height_pixels = $image.Height
		width_pixels = $image.Width
		megapixels = ($image.Height * $image.Width)/1000/1000
		megabytes = (($_.Length)/1024)/1024
		name = $_.Name
		fullname = $_.Fullname
		date = $_.LastWriteTime
        }
    }
} | Export-Csv 'c:\images\img.csv' -NoTypeInformation

Some features of the code:

  • -Filter *.jpg = Find files with a .jpg extension.
  • $image.width -gt 500 = Choose images with a width greater than 500 pixels. (This could be changed to -lt for “less than” or another filter could be used if that’s what’s required). Another option is to use -or instead of -and when defining the full filter.
  • (($_.Length)/1024)/1024 = Convert the file size from bytes to megabytes (a more meaningful measure for most people).
  • ($image.Height * $image.Width)/1000/1000 = Calculate the resolution in megapixels from the height and width parameters.
  • $_.Name = The file’s name.
  • $_.Fullname = The file’s name and path.

Some of these parameters can be omitted, depending on specific requirements.

Listing multiple image types

Unfortunately the -Filter operation can’t be used for multiple file types, so the code needs to be modified if multiple image file types are to be listed:

Get-ChildItem -Recurse c:\images $originalPath -Include @("*.png", "*.jpg") | % {
    $image = [System.Drawing.Image]::FromFile($_.FullName)
    if ($image.width -gt 500 -and $image.height -gt 500) {
        New-Object PSObject -Property @{
		height_pixels = $image.Height
		width_pixels = $image.Width
		megapixels = ($image.Height * $image.Width)/1000/1000
		megabytes = (($_.Length)/1024)/1024
		name = $_.Name
		fullname = $_.Fullname
		date = $_.LastWriteTime
        }
    }
} | Export-Csv 'c:\images\img.csv' -NoTypeInformation

The list of file types can be extended as much as is required.

Don’t forget that -Include operations consume a lot more resources than -Filter, so the script will run more slowly. This could come at quite some cost if many thousands of images are to be searched.

   

Comments:

8 responses to “Script to list image files by size and resolution, export to Excel”

  • Written by Deepak Shukla on 4 November 2016:

    we need script to export list only file name and files resolutions.
    we have 50000 .tif files.
    can help me anyone.

    • Written by Adam Dimech on 8 November 2016:

      So why not use this script? It should work for you, although it may take some time.

  • Written by Aditya Patel on 19 May 2018:

    Just in case if someone gets error that unable to find System.Drawing.Image assembly is missing, use this on the top line of your script
    [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)

    If you get error that ExceptionPolicy is restricted, run powershell as admin user and type this:
    Set-ExecutionPolicy RemoteSigned

  • Written by Kamran on 21 May 2018:

    Cannot find path ‘C:\Windows\System32\WindowsPowerShell\v1.0\System.Drawing.Bitmap’ because it does not exist

  • Written by Joe Peni on 5 May 2019:

    Thanks for the useful Powershell script

  • Written by Venkat on 8 October 2019:

    Before posting this comment i have been searching to get the images dimensions(approximately 450 images), finally i achieved with your script after couple of hours.

    Thanks for the great help.

    Thanks,
    Venkat.

  • Written by Alexandru P on 4 March 2020:

    Thank you! Very usefull the script.

  • Written by Michael Rhodes on 14 May 2020:

    How do you control the column order of the image properties in the CSV?

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.