Get Files Older Than 1 Month with PowerShell

In this tutorial, I will explain how to get files older than 1 month with PowerShell. As a PowerShell expert with over a decade of experience, I’ve frequently needed to find and manage older files on systems. There are multiple methods to find files older than one month.

In PowerShell, the LastWriteTime is the most reliable indicator of when a file was last modified, so we’ll focus on that property in our examples.

Method 1: Basic Command to Find Files Older Than 1 Month

The simplest approach uses PowerShell’s Get-ChildItem cmdlet combined with Where-Object to filter files by date.

$cutoffDate = (Get-Date).AddMonths(-1)
Get-ChildItem -Path "C:\Reports\Folder" -File | Where-Object { $_.LastWriteTime -lt $cutoffDate }

In this command:

  1. We calculate a date exactly one month ago using (Get-Date).AddMonths(-1)
  2. We get all files (not folders) in the specified path
  3. We filter to include only files with a LastWriteTime older than our cutoff date

As shown in the screenshot below, I wanted to check files older than 30 days in my Downloads folder, which displays a list of files.

Get Files Older Than 1 Month with PowerShell

I use this method when I need a quick list of older files without any complex requirements.

Check out Find Files Modified in the Last 24 Hours in PowerShell

Method 2: Find Files Recursively in Subfolders

Often, you’ll need to search through an entire directory structure. Here’s how to modify our command to search recursively:

$cutoffDate = (Get-Date).AddMonths(-1)
Get-ChildItem -Path "C:\Example\Folder" -File -Recurse | Where-Object { $_.LastWriteTime -lt $cutoffDate }

By adding the -Recurse parameter, PowerShell will search through all subfolders automatically. This is particularly useful for complex folder structures like document libraries or application data folders.

When I’m cleaning up our company’s project directories, this recursive approach helps me find forgotten files in nested folders that might otherwise be missed.

Read Find Files Modified Between Dates Using PowerShell

Method 3: Filter by File Type and Age

Sometimes, you only need to find specific file types that are older than a month. Here’s how to filter by both age and file extension:

$cutoffDate = (Get-Date).AddMonths(-1)
Get-ChildItem -Path "C:\Example\Folder" -Include "*.log","*.tmp" -File -Recurse | 
    Where-Object { $_.LastWriteTime -lt $cutoffDate }

This command specifically searches for .log and .tmp files that are older than one month. You can customize the -Include parameter with any file extensions you need to target.

I’ve used this approach to identify old log files across our application servers that were consuming unnecessary disk space.

Method 4: Export Results to CSV for Reporting

When managing files across multiple systems, it’s often helpful to export your results for documentation or review. Here is the PowerShell script to export the file details to a CSV file.

$cutoffDate = (Get-Date).AddMonths(-1)
$oldFiles = Get-ChildItem -Path "C:\Example\Folder" -Recurse -File | 
    Where-Object { $_.LastWriteTime -lt $cutoffDate }
$oldFiles | Select-Object FullName, LastWriteTime, Length | 
    Export-Csv -Path "C:\Reports\OldFiles.csv" -NoTypeInformation

This script creates a CSV report with the file path, last modified date, and size of each file. This is perfect for creating documentation for audits or getting approval before deleting files.

Read Show Progress When Copying Files with PowerShell Copy-Item

Method 5: Create an Advanced Function for Reusability

When you need to perform this task regularly, creating a reusable function saves time:

function Find-OldFiles {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$false)]
        [int]$MonthsOld = 1,

        [Parameter(Mandatory=$false)]
        [string[]]$FileTypes,

        [Parameter(Mandatory=$false)]
        [switch]$Recurse
    )

    $cutoffDate = (Get-Date).AddMonths(-$MonthsOld)
    $params = @{
        Path = $Path
        File = $true
    }

    if ($Recurse) {
        $params.Add('Recurse', $true)
    }

    if ($FileTypes) {
        $params.Add('Include', $FileTypes)
    }

    Get-ChildItem @params | Where-Object { $_.LastWriteTime -lt $cutoffDate }
}

With this function, you can easily find old files with different parameters:

# Find all files older than 1 month in a folder
Find-OldFiles -Path "C:\Example\Folder" -Recurse

# Find PDF files older than 3 months
Find-OldFiles -Path "C:\Documents" -MonthsOld 3 -FileTypes "*.pdf" -Recurse

I’ve implemented similar functions in our company’s IT management scripts, enabling our team to run these operations consistently across different systems.

Check out Delete User Profiles Older Than 30 Days Using PowerShell

Method 6: Check File Age in Days Instead of Months

For more precise control, you might want to specify the age in days rather than months:

$cutoffDate = (Get-Date).AddDays(-30)  # Approximately 1 month
Get-ChildItem -Path "C:\Example\Folder" -File | 
    Where-Object { $_.LastWriteTime -lt $cutoffDate }

This approach is useful when you need more granular control over the timeframe.

Taking Action on Old Files

Once you’ve identified files older than a month using PowerShell, you’ll likely want to take action on them. Here are some common operations that you can try based on your requirement.

1. Moving Old Files to an Archive Location

Sometimes you might want to move the old files to an archive location. Below is the PowerShell script you can use.

$cutoffDate = (Get-Date).AddMonths(-1)
$oldFiles = Get-ChildItem -Path "C:\Example\Folder" -File | 
    Where-Object { $_.LastWriteTime -lt $cutoffDate }

foreach ($file in $oldFiles) {
    $destination = "C:\Archive\" + $file.Name
    Move-Item -Path $file.FullName -Destination $destination
}

This script moves all old files to an archive folder. I’ve used this approach for quarterly archiving of project files.

2. Delete Old Files

You might also want to delete old files after finding them in PowerShell. You can use the below PowerShell script.

$cutoffDate = (Get-Date).AddMonths(-1)
Get-ChildItem -Path "C:\Temp" -File | 
    Where-Object { $_.LastWriteTime -lt $cutoffDate } | 
    Remove-Item -Force

This command removes old files. Always use caution with deletion commands and consider adding -WhatIf for testing before running the actual deletion.

3. Compress Old Files

If your organization does not want to delete the old files, you can suggest that they compress them instead. Here is the PowerShell script to do so.

$cutoffDate = (Get-Date).AddMonths(-1)
$oldFiles = Get-ChildItem -Path "C:\Logs" -File | 
    Where-Object { $_.LastWriteTime -lt $cutoffDate }

Compress-Archive -Path ($oldFiles.FullName) -DestinationPath "C:\Archives\OldLogs.zip"

This compresses old files into a ZIP archive, which is great for logs or other files you need to keep but don’t access frequently.

Check out Create Files with Content Using PowerShell

Best Practices and Tips

Based on my experience, here are some best practices when working with file age in PowerShell:

  1. Always test first: Use -WhatIf with any commands that modify files to preview what would happen.
  2. Be specific with paths: Target specific folders rather than scanning entire drives to improve performance.
  3. Consider file size: When working with large directories, consider filtering by size first:
   Get-ChildItem -Path "C:\Logs" -File | 
       Where-Object { $_.Length -gt 10MB -and $_.LastWriteTime -lt $cutoffDate }
  1. Schedule your scripts: Use Windows Task Scheduler to run your cleanup scripts automatically on a regular basis.
  2. Add logging: Include logging in your scripts to keep track of what files were processed:
   $oldFiles | ForEach-Object { 
       Write-Output "$(Get-Date) - Processed: $($_.FullName)" | 
       Add-Content -Path "C:\Logs\FileCleanup.log" 
   }

Troubleshooting Common Issues

Here are some common issues that you might face while running the PowerShell script.

Problem: Script Running Slowly

If your script is taking too long to run, try:

  • Limiting the depth of recursion with -Depth parameter
  • Processing files in batches
  • Running during off-hours

Problem: Permission Denied Errors

When encountering permission issues:

  • Run PowerShell as Administrator
  • Use the -Force parameter where applicable
  • Check file locks with tools like Handle or Process Explorer

Conclusion

In this tutorial, I explained how to find files older than one month with PowerShell. You learned different methods with examples here.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.