Tag Archives: SupportsShouldProcess

How to Identify and Remove Old Files With PowerShell

Today, I want to share a second PowerShell function, Remove-OldFiles, that shows how to identify and remove old files. Typically, you want such a function in order to keep log and temp folders clean from very old stuff.

Old files, what does that mean? The function investigates the LastWriteTime property from each file found and compares it to a given maximum age. By default, the function will delete all files with a lastwritetime that is over 90 days.

function Remove-OldFiles
{
    <#
    .SYNOPSIS
        Deletes files older than a specified amount of days (default 90).

    .DESCRIPTION
        Deletes files older than a specified amount of days (default 90). If not
        specified elsewhere, the function identifies all files within the current
        directory with a lastwritetime that is over the specified amount of days
        and deletes them.

    .PARAMETER Path
        Specifies the folder where to start (default is current directory)

    .PARAMETER Filter
        Specifies a filter (default is *.*)

    .PARAMETER Recurse
        Recursively process the directory structure

    .PARAMETER Age
        Specifies the maximum count of days between current time and the lastwritetime.

    .EXAMPLE
        PS C:\> "C:\Windows\Temp" | Remove-OldFiles -Recurse -WhatIf

        This command shows a way to safely check which files the function would delete

    .EXAMPLE
        PS C:\> Remove-OldFiles C:\Windows\Temp -Recurse

        This command recursively deletes all old files starting from C:\Windows\Temp
    #>
    
    [cmdletBinding(SupportsShouldProcess=$True)]
    param (
        [Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true)]
        [String[]]
        $Path
        ,
        [Parameter(Mandatory=$false)]
        [String]
        $Filter
        ,
        [Switch]
        $Recurse
        ,
        [Parameter(Mandatory=$false)]
        [Int]
        $Age = 90
    )
    
    # Avoid confusing Get-ChildItem
    $PSBoundParameters.Remove("Age") | Out-Null
    $PSBoundParameters.Remove("WhatIf") | Out-Null
    $PSBoundParameters.Remove("Confirm") | Out-Null
    
    $OldestLastWriteTime = (Get-Date).AddDays(-$Age)
    
    Get-ChildItem @PSBoundParameters |
        Where-Object {$_.PSIsContainer -eq $false} |
        Where-Object {$_.LastWriteTime -lt $OldestLastWriteTime} |
        Remove-Item -Force
}

Disclaimer: I hope that the information in this post is valuable to you. Your use of the information contained in this post, however, is at your sole risk. All information on this post is provided “as is”, without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. Further, I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.