≡ Menu

PowerShell: Find files older than X days or larger/smaller than given size

In today’s article, I will take you through a PowerShell script that helps to get the list of files that are older than a given number of days. This script searches for files in a given directory path and calculates the age of each file. We can apply filters on it get the files older than we need.

The script is simple and self-explanatory, so we are not going to spend much time taking you through part of the scripts. The only one thing I want to highlight is it gives the age of files only and not directories in the path provided to the script.

Below is the script and it has only one mandatory input parameter, Path. You just need to provide the directory path in which you want to search for a list of files older than some days. We can filter the output further down to specify the days we are interested in. You will see this in the examples section below.

Save the below code into Get-Olderfiles.ps1

Code

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [string]$Path
)

#Check and return if the provided Path not found
if(-not (Test-Path -Path $Path) ) {
    Write-Error "Provided Path ($Path) not found"
    return
}

try {
    $files = Get-ChildItem -Path $Path -Recurse 
    foreach($file in $files) {
        
        #Skip directories as the current focus is only on files
        if($file.PSIsContainer) {
            Continue
        }

        $last_modified = $file.Lastwritetime
        $time_diff_in_days = [math]::floor(((get-date) - $last_modified).TotalDays)
        $obj = New-Object -TypeName PSObject
        $obj | Add-Member -MemberType NoteProperty -Name FileName -Value $file.Name
        $obj | Add-Member -MemberType NoteProperty -Name FullPath -Value $file.FullName
        $obj | Add-Member -MemberType NoteProperty -Name AgeInDays -Value $time_diff_in_days
        $obj | Add-Member -MemberType NoteProperty -Name SizeInMB -Value $([Math]::Round(($file.Length / 1MB),3))
        $obj
    }
} catch {
    Write-Error "Error occurred. $_"
}

Example

By default, the script returns a list of objects that shows the files in the given path and their age and size.

.\Get-OlderFiles.ps1 -Path c:\temp\myfiles

Like I said before, we can use filters now to get the files older than given days. Let us say we want files older than 10 days.

.\Get-OlderFiles.ps1 -Path c:\temp\myfiles | ? {$_.AgeInDays -gt 10}

You can do it for any other days you want. 10,60,102.. whatever is the number. It will return an empty output if there are no files older than the specified number. Just keep that in mind.

.\Get-OlderFiles.ps1 -Path c:\temp\myfiles | ? {$_.AgeInDays -gt 60}
.\Get-OlderFiles.ps1 -Path c:\temp\myfiles | ? {$_.AgeInDays -gt 102}

If you want to export the output to a CSV file, you can easily do it by using Export-CSV cmdlet.

.\Get-OlderFiles.ps1 -Path c:\temp\myfiles | ? {$_.AgeInDays -gt 60} | export-CSV c:\temp\myfiles.csv -NoTypeInformation

If you notice closely, the script returns the size of each file as well in MB. You can use the filters in a similar way to get files less than or greater than a given size. For example, the below command finds the files greater than 50Mb and exports the details to a CSV file.

.\Get-OlderFiles.ps1 -Path c:\temp\myfiles | ? {$_.SizeInMB -gt 50} |Export-CSV c:\temp\myfiles.csv -NoTypeInformation.

There are a good number of ways you can use this script. You can even use it to find files less than a given age. Please write it in the comments section below if you have trouble sorting it out. We respond back as soon as we can.

{ 0 comments… add one }

Leave a Comment