GettingStarted
GettingStarted

Reputation: 7625

Deleting Files Older Than 7 Days using PowerShell on Windows Server 2012

$PathToFolder = "D:\SCAN\iPhoneDepartment"
$limit = (Get-Date).AddDays(-7)

Get-ChildItem -Path $PathToFolder -Include *.* -File -Recurse | ? {
  -not $_.PSIsContainer -and $_.CreationTime -lt $limit
}| foreach { $_.Delete()}

This is my code and it runs without any errors but it's not removing files that are older than 7 days.

Upvotes: 2

Views: 1004

Answers (1)

Zeek
Zeek

Reputation: 36

Maybe you're trying to delete files that haven't changed in 7 days?

Get-ChildItem -Path $pathToFolder -File -Recurse | ? LastWriteTime -LE $limit | Remove-Item

Upvotes: 1

Related Questions