2
votes

I'm trying to find files from the previous day to copy, but my simple get-childitem isn't working. It works with every other switch except -eq. Any suggestions to list files only from the previous day ?

get-childitem c:\users| where-object {$_.LastWriteTime -eq (get-date).adddays(-2)}

2

2 Answers

3
votes

You are looking for files that are written at exact time ( hours mins, secs, year, month and 2 days before). Unless the files were written to the second, two (or one) days ago, you will not find them. In other words, you were comparing full DateTime objects and not just a date and hence there is very less chance that they were exactly equal, which seemed to suggest that -eq doesn't work, but other comparisons do.

You probably wanted to just compare the dates, without the times:

$yesterday = (get-date).AddDays(-1).Date
gci c:\users | ?{ $_.LastWriteTime.Date -eq $yesterday}

( also moved the get-date outside, as you probably don't want to do it again and again.)

1
votes

They are not equal because they differ in time. If you want an exact date match, use the Date property:

get-childitem c:\users| where-object {$_.LastWriteTime.Date -eq (get-date).adddays(-2).Date}