Get Files Older Than X Days

This sample defines a PowerShell function that lists files in a directory that haven't been modified for x number of days. The function takes a parameter which specifies the number of days:

  Function Get-FilesXDaysOld([string]$path,[int]$daysOld) {
    Get-ChildItem -Path $path | ?{$_.LastWriteTime.Date -le (Get-Date).AddDays(-$daysOld)}


This function is useful if you want to archive old files or clean up a directory.

Examples of using this sample:

  C:\Windows\system32> Get-FilesXDaysOld "C:\Files\" 10
  C:\Windows\system32> Get-FilesXDaysOld "C:\Files\" 5


© 2019