How Do You Find Files That Were Modified Recently and Have a Specific String in Their Name?

Problem scenario
On a Windows computer you want to find .txt files that were modified within the past four days. Using PowerShell you want to find files in a directory and its subdirectories that were modified within a certain time (within the past four days) and have a pattern in the name of ".txt". How do you search every folder on the computer (or server) to filter files with .txt extensions that were modified within the past four days?  

Solution
This solution assumes there is only one drive and it is the C: drive.  Run this PowerShell command:

Get-ChildItem -recurse -Path 'C:\' -Filter *.txt | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-4) } 

You can change the ".txt" to a different extension.  You can change the "4" to a different number of days.  The command provides a list of files that meet the two requirements (e.g., has .txt in its name and the temporal requirement of having been modified within the past four days).

Leave a comment

Your email address will not be published. Required fields are marked *