Posted On October 9, 2020

PowerShell: Get Date Taken Value of a Photo or Video

kimconnect 0 comments
blog.KimConnect.com >> Windows >> PowerShell: Get Date Taken Value of a Photo or Video

This function only deals with photos. Videos, such as MOV files, have an attribute called ‘media created’ that will be included in this iteration.

function getMediaDateTaken($filePath,$propertyIndex){
    
    if(!$propertyIndex){
        $propertyIndex=switch -regex ($filePath){
            '(JPG|PNG|GIF|BMP|TIFF|JPEG)+$' {12}
            '(MOV|MP4|AVI|MPG|MPEG|WMV|QT|FLV|SWF)+$' {208} # Assuming Windows 10
        }
    }
    try{
        # Alternative method that is less efficient
        #[reflection.assembly]::LoadWithPartialName('System.Drawing')
        #$media = New-Object System.Drawing.Bitmap($mediaFile)
        #$byteArray = $media.GetPropertyItem(36867).Value 
        #$string = [System.Text.Encoding]::ASCII.GetString($byteArray) 
        #$DateTime = [datetime]::ParseExact($string,"yyyy:MM:dd HH:mm:ss`0",$Null)
        #write-host $DateTime
        
        $shell = New-Object -COMObject Shell.Application
        $shellFolder = $shell.Namespace($(split-path $filePath))
        $shellFile = $shellFolder.ParseName($(split-path $filePath -leaf))
        # How to view all attributes: 0..287|%{'{0}: {1}' -f $_,$shellFolder.GetDetailsOf($shellFile,$_)}
        $dateTakenUnicode=$shellFolder.GetDetailsOf($shellFile, $propertyIndex)
        [datetime]$dateTaken=$dateTakenUnicode -replace '[^\d^\:^\w^\/^\s]'

        # The more convoluted method
        #$ascii= [System.Text.Encoding]::ASCII
        #$unicode = [System.Text.Encoding]::UNICODE
        #$dateAscii=([System.Text.Encoding]::Convert($unicode,$ascii,$unicode.GetBytes($dateTakenUnicode))|%{[char]$_}) -join ''
        #$dateTaken=$dateAscii -replace '[^\d^\:^\w^\/^\s]'
        return $dateTaken
    }catch{
        return $false
    }
}

Leave a Reply

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

Related Post

How to Restart Domino Services without Reboot

--------------Restart_Domino.bat------------------------------------------------------net stop "Lotus Domino Server (LotusDominoData)"c:\bats\pulist | findstr /I /C:"nadminp.exe" >c:\bats\pid.lstc:\bats\pulist | findstr /I /C:"naldaemn.exe"…

How to move the Active Directory (AD) Global Catalog (GC) to another domain controller (DC)

You don't actually move the GC between servers. Instead, you simply enable the GC on…

Proposal: Network Optimization (Simplified)

Kim ConnectNetwork Optimization Proposal June 23, 2017 Overview 1.    Project Background and Description We are…