How to rename a large amount of photographs with Windows Powershell using exif data?

Roberto 0 Reputation points
2024-11-19T12:36:17.27+00:00

Hi to all,

as in the subject I would like to ask for help on how to rename a large amount of photographs using Windows Powershell. Specifically I was currently using this string:

" Get-ChildItem | Rename-Item -NewName {"IMG_" + $.LastWriteTime.ToString("yyyyMMdd_HHmmss") + ($.Extension)} "

The problem is that I need to use the exif data of the photographs, in particular I need to use the "Acquisition date" and the "LastWriteTime" parameter is not correct. I would like the output file name to be the following: IMG_yyyyMMdd_HHmmss.jpg where for "yyyyMMdd_HHmmss" I would like the acquisition date to be used. Can someone please help me?

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,401 Reputation points
    2024-11-19T13:51:55.63+00:00

    Here's a script that I hacked together a few years ago that you can modify to do that. In my case, the file names were ok, but I wanted to modify the LastWriteTime to match the exif date.

    I was looking for "Date taken" and "Media Created", but you can modify that for "Acquisition date" if that's the exif value in your files.

    $folder = 'c:\temp\zzzzevan\test'
    cls
    $shell = New-Object -COMObject Shell.Application
    $files = Get-ChildItem -Path $folder 
    $shellfolder = $shell.Namespace($folder)
    foreach ($f in $files) {
        ""
        $f.name 
        $PicDate = "" 
        $shellfile = $shellfolder.ParseName($f.name )
        # To find the ID of the extended attribute you're interested in:
        0..287 | ForEach-Object {
            $prop = $shellfolder.GetDetailsOf($null, $_)
            $date = $shellfolder.GetDetailsOf($shellfile, $_)
            if ($shellfolder.GetDetailsOf($null, $_) -eq "Date taken") {        
                if ($date -ne "") { 
                    $PicDate = $date
                    '{0} = {1} = {2}' -f $_, $prop, $date
                }
            }
            if ($shellfolder.GetDetailsOf($null, $_) -eq "Media created") {   
                if ($date -ne "") { 
                    $PicDate =  $date
                    '{0} = {1} = {2}' -f $_, $prop, $date
                }             
            }
        }
        if ($PicDate -ne "") {
            $PicDate = ($PicDate.ToCharArray() | foreach {if ($_ -match '([\d:/apm ])') {$Matches[0]}}) -join ""
            "Current LastWriteTime date is {0}" -f $f.LastWriteTime
            "Set it to $PicDate"
            
            # Select which time value you want to update 
            #$f.LastWriteTime = [datetime]$PicDate         
            #$f.CreationTime = [datetime]$PicDate
            # Rename the $f file here. 
        }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.