Finding files modified after a given timestamp

If you want to work on files in a directory structure that were only modified since before or after a specific timestamp, you can use the below methodology to return a list of the matching items.  This is useful if you are processing data files in batches based on date/time cut offs but are not able to move or rename the processed files in a given folder.

First you need to create a variable for your date cut off.  This will be the date/time before or after which you wish to return matching files for.  For this example, we want a cut off watermark of 9/20/2008 at midnight.

$Watermark = get-date -Date 9/20/2008 -Hour 0 -Minute 0 -Second 0

Next you will need to use get-childitem to return a list of the files that were modified after this watermark for further action or reporting.  In this case, this command is intended to execute from a top level folder and return all modified files in any folder beneath that starting point.  The where clause here is exluding folders that were modified after a given date but not the files within them, as my script needed to process files and not folders.  Obviously you can work with the filter parameters to suit your specific requirements.

$NewFiles = get-childitem -recurse | where {$_.psiscontainer -eq $false -and $_.LastWriteTime -ge $watermark}
This entry was posted in PowerShell. Bookmark the permalink.