Delete Files Older than X Days in PowerShell

Delete Files older than X days in PowerShell

Delete Files Older than X Days in PowerShell

Use Remove-Item cmdlet with the Get-ChildItem cmdlet and Where-Object cmdlet to delete files older than X days.

The above script retrieved all the items from the given path, filtered them based on the specified property and kept only those files/folders that were less than the given number of days. Then, finally, all the filtered items were removed. Let’s understand the above script in detail below.

First, we created and initialized a variable named $path to keep the location where our files and folders live. Then, we created another variable, $numberOfDays, which contained the number of days, as is evident from the variable’s name.

Next, we used Get-ChildItem to retrieve all the items and child-items from the given $path. If any items are a container, this cmdlet will get all items inside that container; we refer to these items as child items. Note that we can use the -Recurse parameter to access items in every child container and the -Depth parameter to limit the number of levels to recurse. You can read about both parameters here.

After that, we piped the Get-ChildItem cmdlet with the Where-Object cmdlet to filter the retrieved items based on the LastWriteTime property; this property represents the last write time for every item in the pipeline. The $_ is an automatic variable denoting the pipeline’s current item. Then, we used Get-Date to get the current date & time and .AddDays(-$numberOfDays) to subtract -$numberOfDays from it.

Now, you might be thinking that how an .AddDays() can subtract, it should add. Since we have negated the $numberOfDays while passing it to the .AddDays() method, adding $numberOfDays to the current date is equivalent to subtracting -$numberOfDays from the current date.

We used a less than (represented with lt) comparison operator to assess if the .LastWriteTime property is less than the current date minus $numberOfDays. If this results in true, the current item will be added to the output of the Where-Object cmdlet; otherwise, the current item will be excluded. Finally, we used the Remove-Item to delete all the items received from the Where-Object cmdlet.

Delete Files Older than X Days in directory and sub-directories in PowerShell

Use the Remove-Item Cmdlet with -Include and -Recurse parameters to delete files older than X days.

This script will delete all the files from the given directory and sub-directories that are older than 27 days.

We have already learned about $path, $numberOfDays, Remove-Item, Where-Object, LastWriteTime, Get-Date, and .AddDays(). Here, we will learn about options and parameters followed by Remove-Item and preceded by the pipeline. In simple words, we will be talking about the Remove-Item $path\* -Recurse -Include *.* -Force expression.

The Remove-Item removed all the items from the specified $path; having \* after the $path means all files in the $path must be deleted. Next, we used the -Recurse parameter to delete files from the sub-directories while -Include *.* included all files irrespective of their extension. Finally, the -Force was also used to delete read-only files.

That’s all about how to delete files older than X days in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *