How to Delete Files Older Than X Days in PowerShell?

If you want to maintain and organize files, then it is good to regularly delete files that are no longer needed, specifically files that are older than a certain number of days. In this tutorial, I will show you how to delete files older than X days in PowerShell.

To delete files older than a specified number of days in PowerShell, use the Get-ChildItem cmdlet to retrieve the files from a directory, filter them by last write time with the Where-Object cmdlet, and then remove them with Remove-Item. For example, to delete files older than 30 days, you could use this command: Get-ChildItem -Path “C:\MyFolder” | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item.

Delete Files Older Than X Days in PowerShell

PowerShell provides Get-ChildItem cmdlet to delete files older than a certain number of days.

When using this command, it first retrieves the files from a specified directory and then piping the output to the Where-Object cmdlet to filter the files based on their age. Finally, the Remove-Item cmdlet is used to delete the files.

Here’s a basic example of a script that deletes files older than 30 days from a specific folder:

$Path = "C:\MyFolder"
$Days = 30
Get-ChildItem -Path $Path | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) } | Remove-Item

In this script, $Path specifies the directory to search for files, and $Days defines the number of days used to filter files based on their last write time. Files older than 30 days from the current date are deleted.

In the screenshot below, I executed the script, which deleted all the files older than 30 days from the directory.

Delete Files Older Than X Days in PowerShell

Read How to Use PowerShell to Find Files Modified After a Certain Date?

Delete Specific File Types Older Than 30 Days in PowerShell

You can also modify the PowerShell script to delete specific file types or exclude certain directories that are older than 30 days. For instance, to delete only .log files older than 30 days, you can use the following script:

$Path = "C:\Logs"
$Days = 30
Get-ChildItem -Path $Path -Filter *.log | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) } | Remove-Item

This PowerShell script will search for .log files specifically and exclude all other file types.

Handling Errors and Logging

It is always good to handle errors while writing the PowerShell script. You can add -ErrorAction and -ErrorVariable parameters to capture any errors that occur. Additionally, you can output the actions to a log file using Out-File or Add-Content cmdlets. Here’s how you can modify the PowerShell script to include error handling and logging:

$Path = "C:\MyFolder"
$Days = 30
$LogPath = "C:\Logs\log.txt"
$ErrorActionPreference = "Continue"

Get-ChildItem -Path $Path -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$Days) } | ForEach-Object {
    try {
        $_ | Remove-Item -ErrorAction Stop
        "$($_.FullName) was deleted successfully" | Out-File -FilePath $LogPath -Append
    } catch {
        "An error occurred: $_" | Out-File -FilePath $LogPath -Append
    }
}

In the above PowerShell script, $LogPath specifies the path to the log file where the actions will be recorded. The try block attempts to delete the file and logs the success message, while the catch block captures any errors and logs them.

Conclusion

Using PowerShell to delete files older than a set number of days is easy and most convenient.

In this PowerShell tutorial, I have explained how to delete files older than X days in PowerShell using Get-ChildItem and Remove-Item cmdlets.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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