Q

How do I use a PowerShell filter to identify files by date?

Our expert answers one of the most common questions he hears from admins about using a PowerShell filter to find a file's accurate date.

Asking how to use a Windows PowerShell filter to find objects by date is a pretty common request from IT administrators....

It often comes in the form of, "I need to identify all files older than a certain date."

The first step is to establish your date:

$date = (Get-Date).AddDays(-90)

This gets the current date in a parenthetical command. The resulting date object has an AddDays() method, and adding a negative 90 days gives you the date from 90 days ago. This method accounts for leap years, months of varying lengths and all the other variables, so it'll always return an accurate date.

In PowerShell v3, you can then use Get-ChildItem to retrieve a list of files. Use –recurse to include subfolders and then –file (which is a new parameter in v3) to only return files:

Get-ChildItem –Path C:\Logs\ -Recurse –File

These file objects have a variety of date-related properties, including LastAccessTime. Simply filter on that:

Get-ChildItem –Path C:\Logs\ -Recurse –File |
Where-Object –Filter { $PSItem.LastAccessTime –lt $date }

Note that in PowerShell v2, $PSItem must be replaced with $_. You'll also have to compensate for the missing –File parameter in v2:

Get-ChildItem –Path C:\Logs\ -Recurse |
Where-Object –Filter { -Not ($_.PsIsContainer) } |
Where-Object –Filter { $_.LastAccessTime –lt $date }

PsIsContainer is $True for directories and $False for files. Either way, the output of these commands will be files whose LastAccessTime is less than (older than) 90 days ago. You can then do whatever you want with them.

About the author
Don Jones is a well-known and respected PowerShell expert and educator. He's co-author of three books on PowerShell (see PowerShellBooks.com for a list). You can find his content online, including his PowerShell Q&A forums, by visiting DonJones.com.

This was last published in February 2013

Dig Deeper on Windows PowerShell Scripting

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

An easier way might be to install DOSBOX, and the use the command :
Dir *.* /A-D /O-D which gets you all files, skipping Directories, newest files first.
Or if you want all Excel files, you'd enter:
Dir *.xls? /A-D /O-D.
Easy peasy! Do you understand the Powershell command language? I don't.
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close