PowerShell cmdlet script to delete old files based on the file's extension (e.g. "*.pdf").

<#
.SYNOPSIS

    Delete old files.

.DESCRIPTION

    Deletes the specified files based on the file's extension (e.g. "*.pdf")
    and creation date.

    Files are deleted recusively, starting from the specified "-DeleteFilesPath"
    parameter.

.EXAMPLE

    To delete PDF files older than 7 days in the specified -DeleteFilesPath.

        .\cleanup-old-files.ps1 `
            -DeleteFilesPath "C:\path\to\target\dir" `
            -FileFilter "*.pdf" `
            -MaxAgeAsDays 7

.EXAMPLE

    To show which files would be deleted (dry-run):

        .\cleanup-old-files.ps1 `
            -DeleteFilesPath "C:\path\to\target\dir" `
            -FileFilter "*.pdf" `
            -MaxAgeAsDays 7 `
            -WhatIf
#>
[CmdletBinding(SupportsShouldProcess=$true)]
    param
    (
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $DeleteFilesPath,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $FileFilter,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [int] $MaxAgeAsDays = 7
    )
Get-ChildItem -Path $DeleteFilesPath -File -Filter $FileFilter -Recurse -Force -ErrorAction SilentlyContinue | `
    where CreationTime -lt (Get-Date).AddDays(-$MaxAgeAsDays) | `
    Remove-Item -Force -ErrorAction SilentlyContinue


# -------------------------------------------------------------------------------------
# Call/run delete-old-files.ps1 from bat/cmd script:
#
# Uncomment the following snippet and save the file as a bat script (.bat or .cmd ext)
# to call the "delete-old-files.ps1" powershell script with predefined
# paramaters, and double-click to run support.
# -------------------------------------------------------------------------------------

# @echo off
#
# @rem
# @rem Delete PDF files older than 7 days from "C:\path\to\target\dir"
# @rem
# @rem NOTE: You must remove the "-WhatIf" option from the command below
# @rem to perform the actual deletion.
# @rem
#
# powershell.exe -ExecutionPolicy Bypass -NoProfile -NoLogo ^
#     -Command "& '%~dp0delete-old-files.ps1' -DeleteFilesPath 'C:\path\to\target\dir' -FileFilter '*.pdf' -MaxAgeAsDays 7 -WhatIf"