How can I get programmatic access to the "Date taken" field of an image or video using powershell?

Viewed 15326

I'm trying convert a bunch of pictures and videos, but when I convert it to a new format I obviously lose the properties of the original file. I'd like to be able to read the "Date taken" property from the old file and update it on the new one using powershell.

4 Answers

This works for me, thanks to the above help and others.

try{
Get-ChildItem C:\YourFolder\Path | Where-Object {$_.extension -eq '.jpg'} |
ForEach-Object {
         
        $path = $_.FullName
        Add-Type -AssemblyName System.Drawing
        $bitmap = New-Object System.Drawing.Bitmap($path)
        $propertyItem = $bitmap.GetPropertyItem(36867) 
        $bytes = $propertyItem.Value 
        $string = [System.Text.Encoding]::ASCII.GetString($bytes) 
        $dateTime = [DateTime]::ParseExact($string,"yyyy:MM:dd HH:mm:ss`0",$Null)

        $bitmap.Dispose()

        $_.LastWriteTime = $dateTime
        $_.CreationTime = $dateTime
        }}
finally
{
}

To read and write the "date taken" property of an image, use the following code (building on the answer of @EBGreen):

try
{
    $path = "C:\PATH\TO\SomePic.jpg"
    $pathModified = "C:\PATH\TO\SomePic_MODIFIED.jpg"

    Add-Type -AssemblyName System.Drawing
    $bitmap = New-Object System.Drawing.Bitmap($path)
    $propertyItem = $bitmap.GetPropertyItem(36867) 
    $bytes = $propertyItem.Value 
    $string = [System.Text.Encoding]::ASCII.GetString($bytes) 
    $dateTime = [DateTime]::ParseExact($string,"yyyy:MM:dd HH:mm:ss`0",$Null)
   
    $dateTimeModified = $dateTime.AddDays(1) # Set new date here
    $stringModified = $dateTimeModified.ToString("yyyy:MM:dd HH:mm:ss`0",$Null)
    $bytesModified = [System.Text.Encoding]::ASCII.GetBytes($stringModified) 

    $propertyItem.Value = $bytesModified
    $bitmap.SetPropertyItem($propertyItem)
    $bitmap.Save($pathModified)
}
finally
{
    $bitmap.Dispose()
}
Related