PowerTip: Find Files Modified During a Date Range by Using PowerShell

Summary: Use Windows PowerShell to find files that were modified during a specific date range.

Hey, Scripting Guy! Question How can I use Windows PowerShell to find all files modified during a specific date range?

Hey, Scripting Guy! Answer Use the Get-ChildItem cmdlet to collect the files, filter it to the Where-Object cmdlet, specify the date for the LastWriteTime property, and set greater than and less than parameters:

dir | ? {$_.lastwritetime -gt '6/1/13' -AND $_.lastwritetime -lt '6/11/13'}

Note   dir is an alias for the Get-ChildItem cmdlet, and the ? character is an alias for the Where-Object cmdlet. Windows PowerShell automatically converts ‘6/1/13’ into a date for this operation.

Comments (4)

  1. Sean "The EnergizedTech" Kearney says:

    This is my favorite Cmdlet.   It was the first one I really used in Windows Powershell.  Combined with a REMOVE-ITEM -what and the .adddays() method I was able to remove log files over 24 hours left behind by a vendor application.

    The reason I loved it?

    1. It was an easy to implement solution I found on "ScriptingGuys"

    2. the -WHATIF parameter allowed me to work without a Development environment available

    3. I got to go home early on a Friday with 15 minutes of work and 2 hours billed.

    4. I wrote a song called "Highway to Powershell" on the way home from this early day 🙂

    Sean "I <3 Powershell"

  2. Iro says:

    Is there way to make this recursive… check all sub-directories

  3. Denis Couto says:

    Really handy! tks! 🙂

    Iro, you can just add -recurse to Dir, like "dir -recurse | …"

  4. Ed says:

    Is it possible to narrow it down by time? say I want to delete all files that were modified between 4pm-6pm in all sub folders on a specific day?

Skip to main content