script deleted newer files?

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.

This forum is a space to discuss coding in PowerShell, and technical issues related to development.

- Question about a licensed SAPIEN product? Post here: Product Support for Registered Users
- Question about a trial SAPIEN product? Post here: Former and Future Customers - Questions
This topic is 1 year and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
gqninten
Posts: 1
Last visit: Thu Sep 07, 2023 2:57 pm

script deleted newer files?

Post by gqninten »

Hello All - appreciate the time. hope all is well. need some help in deciphering why a script deleted data when it shouldn't have. The script below is to look for files older than 3 years and then delete it based on LastWriteTime. However there were some files/folders which were modified last week but were deleted.

I thought maybe it's because the 2nd part of the script is to look for empty folders.. however there was content in the deleted folders. so not sure what to make of it.

Is my logic incorrect?



#####

$Folder = "D:\"



#Delete files older than 3 years

Get-ChildItem $Folder -Recurse -Force -ea 0 |

? {!$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-1095)} |

ForEach-Object {

$_ | del -Force -Recurse -Confirm:$false

$_.FullName | Out-File C:\data\3years+-D_Drive.txt -Append

}



#Delete empty folders and subfolders

Get-ChildItem $Folder -Recurse -Force -ea 0 |

? {$_.PsIsContainer -eq $True} |

? {$_.getfiles().count -eq 0} |

ForEach-Object {

$_ | del -Force -Recurse -Confirm:$false

$_.FullName | Out-File C:\data\3years+-D_Drive.txt -Append

}
This topic is 1 year and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked