user523768
user523768

Reputation: 23

Powershell 2 Select & Display Filename by Creation Date

I'm trying to find all instances of files with names like VAT*.xls where the creation date is less than 6 months ago. I have tried:

dir c:\vat*.xls -r | ? {($now -$_.lastwritetime).days -lt 300}

and

gci c:\vat*.xls -r | ? {($now -$_.lastwritetime).days -lt 300}

I know there is a file VAT0210.xls dated 1 April 2010 but neither query gives me that answer. What should it be?

Upvotes: 1

Views: 3207

Answers (1)

Joey
Joey

Reputation: 354436

I'm slightly confused from your question. First you write that you want to get files where the creation date is less than six months ago, then you compare with LastWriteTime. And then you say you want six months but compare with 300 days.

I'm following your prose here in the hope that was the correct one.

Get-ChildItem C:\ -Recurse -Include vat*.xls |
    Where-Object { $_.CreationTime -gt (Get-Date).AddMonths(-6) }

As you can see, it's fairly straightforward. Rewritten with aliases:

ls C:\ -r -i vat*.xls | ?{$_.CreationTime -gt (date).AddMonths(-6)}

If my initial guess as for your intentions was incorrect, feel free to adapt the code – should be trivial now.

Upvotes: 3

Related Questions