-LT is not working...Why?

Hello, I’ve been working on a personal project for over a month now and I am running into a problem.

Basically, I 'd like to have .txt file less than seven days old to be move to a different drive on Sundays. Using Task Scheduler, I’ll have the action run a script I’ve made for PowerShell ISE. The problem is, the script is pulling all .txt files regardless of age. I need only the most recent (7) days. Here is what I’ve come up with doing my own research. I appreciate any help. Thanks!

get-childitem -Path N:\Fake\Source\Location*.txt |
Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(7)} |
copy-item -Destination ‘\Storage\Destination\Test’

Let’s break down the logic together.

Get-date equals today.

Today plus 7 days is 7 days in the future.

Files with lastwritetime -lt (older than) 7 days in the future.

That will be all files unless you have a time machine. I think what you are looking for instead is

(Get-Date).AddDays(-7) #7 days ago

And

-gt # newer than

1 Like