Public/Get-specOldFiles.ps1
Function Get-specOldFiles { <# .SYNOPSIS Retrieves files of a specified type older than a specified number of days in the given path(s) and subdirectories. .DESCRIPTION The Get-specOldFiles function identifies files of a specified type that are older than a specified number of days within the provided path(s) and all subdirectories. It is designed to handle various file types such as 'txt', 'log', 'log.*', 'tmp', 'temp', 'ps1'. .PARAMETER Path Specifies the path(s) where files will be searched. This parameter is mandatory and supports pipeline input. .PARAMETER FileType Specifies the type of files to search for. Valid values include 'txt', 'log', 'log.*', 'tmp', 'temp', 'ps1'. This parameter is mandatory. .PARAMETER DaysOldOrMore Specifies the number of days that determine whether a file is considered old. Files older than this specified number of days will be retrieved. This parameter is mandatory. .EXAMPLE Get-specOldFiles -Path "C:\Logs" -FileType "log" -DaysOldOrMore 7 Retrieves log files in the "C:\Logs" directory that are older than 7 days. Also includes any log files found in subdirectories of C:\Logs. .EXAMPLE "C:\Archives", "D:\Backup" | Get-specOldFiles -FileType "txt" -DaysOldOrMore 30 Retrieves text files in the "C:\Archives" and "D:\Backup" directories that are older than 30 days. Also includes any text files found in subdirectories of C:\Archives and D:\Backup. .NOTES Author : owen.heaume Version : 1.0 #> [cmdletbinding()] param ( [Parameter(Mandatory = $true, ValueFromPipeline = $true)] [string[]]$Path, [Parameter(Mandatory = $true)] [ValidateSet('txt', 'log', 'log.*', 'tmp', 'temp', 'ps1')] [string]$FileType, [Parameter(Mandatory = $true)] [int]$DaysOldOrMore ) process { foreach ($CurrentPath in $Path) { if (Test-Path $CurrentPath -PathType Container) { if ($FileType -eq 'log.*') { $Files = Get-ChildItem -Path $CurrentPath -Filter "*.$FileType" -Recurse -Exclude "*.log" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$DaysOldOrMore) } } else { $Files = Get-ChildItem -Path $CurrentPath -Filter "*.$FileType" -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$DaysOldOrMore) } } return $Files } else { Write-Host "Path $CurrentPath does not exist" -ForegroundColor DarkYellow } } } } |