Playing with EXIF in Powershell

A friend of mine had a lot of pictures he wanted to do some statistics on. I therefore made a few powershell pieces to get him started. Everything is build on top of the output of the great free tool EXIFTool

Here are some example commands:

# Call exiftool to collect all exifdata from a directory, recursively and save it in a file... Important because this process is slow!
C:\temp\exiftool.exe 'Z:\Pics' -CSV -r -EXT NEF CRW JPG -ISO -ISOSetting -Aperture -ExposureTime -Model -Lens -FocalLength -LensID -ExposureCompensation -MeteringMode -Flash -FocusMode -AFAreaMode -CreateDate > c:\temp\all_exif.csv

# Load the exifdata to a variable for further manipulation
$exif = Import-Csv c:\temp\all_exif.csv

# Print different statistic counts
$exif | group ISOSetting
$exif | group Aperture | select Count, Name

# Show all lenses ever used
$exif | group LensID | select Name | sort Name

# Show all files with iso 800
$exif | Where-Object { $_.ISOSetting -eq "800" } | select SourceFile
# Show files with aperture bigger than 11
$exif | Where-Object { ($_.Aperture -as [int]) -gt 11 } | sort Aperture | select SourceFile, FocalLength, Aperture, LensID

# Find the most used combination of ISO and Aperture
$exif | group ISO, Aperture | Sort-Object count -Descending | select Count, name

# Find files with focal length under 20 and ISO greater than or equal 800
$exif | where {
    $_.FocalLength.Replace(" mm", "") -as [int] -lt 20 -and
    $_.ISO -as [int] -ge 800
} | select ISO, FocalLength, SourceFile | sort FocalLength | more

For the lazy ones that don’t want to use powershell… Export it all to excel with the very first command! Remember things takes time. On my system I was able to process about 85000 pictures/hour with the exifutils command!

Phil Harvey (the author of exiftools) pointed out that using json is better for the memory consumption. This can be done by changing the first couple of lines to:

# Call exiftool to collect all exifdata from a directory, recursively and save it in a file... Important because this process is slow!
C:\temp\exiftool.exe 'Z:\Pics' -json -r -EXT NEF CRW JPG -ISO -ISOSetting -Aperture -ExposureTime -Model -Lens -FocalLength -LensID -ExposureCompensation -MeteringMode -Flash -FocusMode -AFAreaMode -CreateDate > c:\temp\all_exif.json

# Load the exifdata to a variable for further manipulation
$exif = (get-content C:\temp\all_exif.json | ConvertFrom-Json)

 

3 thoughts to “Playing with EXIF in Powershell”

  1. Thanks for the tips Alex, there is very little online for ExifTool with PowerShell and your method of exporting and reimporting a data object works nicely.

    Here is an alternative method based on your code, with a folder browser, wait for the process to complete and loads the results back into PowerShell

    # Analyse Exif tags for all files in a folder

    Function browse-to-folder ([string]$message){

    $FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog

    $FolderBrowserDialog.Description = $message

    $FolderBrowserDialog.RootFolder = "MyComputer"

    if ( $FolderBrowserDialog.ShowDialog() -eq "OK" ) { $FolderBrowserDialog.Dispose() ; Return $FolderBrowserDialog.SelectedPath }

    else { Exit }

    }

    Set-Location $PSScriptRoot

    $location = browse-to-folder -message "Select Folder"

    $process = Start-Process -FilePath ".\exiftool.exe" -ArgumentList "-r -a -u -g0 ""$location"" -json" -Wait -RedirectStandardOutput exiftool_output.json -PassThru -NoNewWindow

    $process.WaitForExit()

    $exif_data = (get-content .\exiftool_output.json | ConvertFrom-Json)

    1. Hi Mark,

      Thanks for the input 🙂 I am certain it can be usefull to other people 🙂

      Best regards,
      Alex

  2. Hello Gentlemen,

    Hope this comment finds you both well. I dug this post because I’m interested in using Powershell and ExIfTool to manupulate file names given a specific term form the metadata:

    I have a folder containing 1000+ PDF files with generic names (i.e. DOC (1)) that I need to rename with the order number, which is listed inside the file itself. I’ve had to manually open each document, look for the order number, then change the file name.

    I have a spreadsheet list of all of the order numbers contained in that folder. Luckily, the order number is stored as metadata for each file, so I’ve used the search function of the file explorer to look up each individual order #, then renaming whichever file comes up. This has reduced the task time substantially but it’s still very time-consuming.

    Thank you in advance for your time!

Leave a Reply

Your email address will not be published. Required fields are marked *