troubleshooting Question

Powershell Delete items older than question

Avatar of bjennings
bjennings asked on
Powershell
2 Comments1 Solution17 ViewsLast Modified:
Hi,
I have a powershell script that removes files inside a folder older than a certain number of days. It works on some files and not others and I cannot figure out why it won't delete certain files. Here is the script.
$OlderThan = (get-date).AddDays(-2) #Change number to whatever is necessary
$ParentFolder = "redacted" #Change to necessary Directory

get-childitem $ParentFolder -recurse | where {$_.lastwritetime -lt $olderthan} |% {Remove-Item $_.FullName -force -Recurse} 

Open in new window


So the wierd part is if I run the first part of the script...It does list all the files I expect to be deleted. But when I run the entire thing with the -WhatIf switch, it only will delete one of the files. Please see the below image   powershell snippet
ASKER CERTIFIED SOLUTION
bjennings

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros