Fiilli
Fiilli

Reputation: 25

Comparing last modified TIME of files in Powershell

I am currently trying to find out which file out of two was modified later. Unfortunately Powershell seems to compare only dates (which can be the same), ignoring the hour of last edition. I tried to find solution, but everything I found was only about date comparison, not time.

What I currently have is this (there will always be only one xlsx file and only one csv, so I didn't bother with the fact, that the solution I found for this uses Foreach, but if you can tell me how to get this data without Foreach, would be great too):

Get-Item $OneDrivePathXlsx | Foreach {$_.LastWriteTime}
$LastModifiedXlsx = $_.LastWriteTime
Get-Item $OneDrivePathCsv | Foreach {$_.LastWriteTime}
$LastModifiedCsv = $_.LastWriteTime

$LastModifiedXlsx -gt $LastModifiedCsv

What I get is:

den 7 april 2023 12:50:25
den 7 april 2023 12:34:32
False

even if the first date and TIME is definitely greater than the second one.

How can I make it comparing the time too?

Thanks!

Upvotes: 0

Views: 70

Answers (1)

Olaf
Olaf

Reputation: 5252

This should be enough:

$LastModifiedXlsx = Get-Item $OneDrivePathXlsx
$LastModifiedCsv = Get-Item $OneDrivePathCsv

$LastModifiedXlsx.LastWriteTime -gt $LastModifiedCsv.LastWriteTime

Upvotes: 1

Related Questions