31 lines
1.0 KiB
PowerShell
31 lines
1.0 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Remove files older than the specified number of days.
|
|
|
|
.DESCRIPTION
|
|
This script will delete all files in a directory which are older than the specified number of days.
|
|
It will not search recursively for files and will only look in the directory specified.
|
|
|
|
.PARAMETER
|
|
-Path
|
|
This is the directory to search for old files
|
|
|
|
.PARAMETER
|
|
-OlderThan
|
|
This is the number of days that limits the files that are deleted.
|
|
Anything newer than the value specified here will be retained, while anything older than the value specified here will be deleted.
|
|
|
|
.EXAMPLE
|
|
Remove-OldFiles -Path "C:\Windows\TEMP" -OlderThan 30
|
|
This will remove all files in C:\Windows\TEMP that are older than 30 days
|
|
#>
|
|
|
|
Param(
|
|
[string]$Path="C:\Users\dyoder\Desktop",
|
|
[int]$OlderThan=7
|
|
)
|
|
|
|
If ( Test-Path $Path ) {
|
|
$files = Get-ChildItem $Path | Where { !$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-${OlderThan}) }
|
|
foreach ( $f in $files ) { write-host $f.FullName }
|
|
} Else { Write-Host "The path does not exist: ${Path}" } |