scripts/Remove-OldFile.ps1

function Remove-OldFile {

    [CmdletBinding(
        DefaultParameterSetName = 'Path',
        SupportsShouldProcess,
        ConfirmImpact = ([System.Management.Automation.ConfirmImpact]::Medium))]

    param(
        [Parameter(ParameterSetName = 'Path', Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
        [SupportsWildcards()]
        [string[]] $Path,

        [Parameter(ParameterSetName = 'LiteralPath', Mandatory, Position = 0, ValueFromPipelineByPropertyName)]
        [Alias('PSPath')]
        [Alias('FullName')]
        [string[]] $LiteralPath,

        [Parameter(Mandatory)]
        [ValidateSet('CreationTime', 'LastWriteTime', 'LastAccessTime')]
        [string] $PropertyToCompare,

        [Parameter(Mandatory)]
        [datetime] $DateTimeToCompare,

        [switch] $Force)

    if (!$Force) {
        if ($Path) {
            $processTarget = $Path
        }
        else {
            $processTarget = $LiteralPath
        }

        if (!$PSCmdlet.ShouldProcess($processTarget, "Delete files which $PropertyToCompare property is older than $DateTimeToCompare")) {
            return
        }
    }

    $getItemParams = @{}

    if ($Path) {
        $getItemParams['Path'] = $Path
    }
    else {
        $getItemParams['LiteralPath'] = $LiteralPath
    }

    $items = @(Get-Item @getItemParams)

    $containerItems = $items | Where-Object -FilterScript { $_.PSIsContainer }
    foreach ($containerItem in @($containerItems)) {
        Write-Warning "$($containerItem.FullName) is ignored because it's a container."
    }

    $removeParams = @{
        Verbose = [bool] $PSBoundParameters['Verbose']
    }

    $leafItems = $items | Where-Object -FilterScript { !$_.PSIsContainer }
    $leafItems |
        Where-Object -Property $PropertyToCompare -LT $DateTimeToCompare |
        Remove-Item @removeParams -Force
}