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
}
}
Categories: