I'm pulling out a date from a file using the below code. The value looks like it is a string, but when you use the ParseExact (see code with comment) then it fails. If I create a variable of the same value and do the same ParseExact it then works. So I'm trying to debug the issue and it appears that both values are the same, but they can't be as one works while the other doesn't.
function getDateFromFile($file) {
$shellObject = New-Object -ComObject Shell.Application
$directoryObject = $shellObject.NameSpace( $file.Directory.FullName )
$fileObject = $directoryObject.ParseName( $file.Name )
$property = 'Date taken'
for(
$index = 5;
$directoryObject.GetDetailsOf( $directoryObject.Items, $index ) -ne $property;
++$index ) { }
$value = $directoryObject.GetDetailsOf( $fileObject, $index )
$format= "dd/MM/yyyy H:mm";
# $value when debugging appears to be -> "20/04/2021 14:04"
#The below fails -> Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
$date01=[System.DateTime]::ParseExact($value,$format, $null)
# The below works fine and $date02 becomes -> 20 April 2021 14:04:00
$tmp = "20/04/2021 14:04"
$date02=[System.DateTime]::ParseExact($tmp,$format, $null)
return $date1=[System.DateTime]::ParseExact($tmp,$format, $null)
}
I've added comments into the above snippet. So how do you debug powershell to be able to tell what's wrong (other than standard breakpoints with powershell LSE)? If you do know what's wrong, as well as pointing out how to fix it, it would be good to know how you know.
Update - This is a similar question How can I get programmatic access to the "Date taken" field of an image or video using powershell? as I had already used some of the code from an answer there. But as mentioned below in the comments and in this Q the returned string isn't usable and I needed to know how to debug it.