Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Delete files from specific folder and older than X days

$
0
0

Hello guys,

I'm trying to create a script that searches and delete files older than 2 days inside 2 different Root Folders' subfolders with Specific name. So far, what I did is this script below, but it is not working :(

$limit = (Get-Date).AddDays(-2)
$paths = "D:\work\AppLog1,G:\Logs\AppLog1" -split ','

#Delete files from cdatalogs folder
foreach($path in $paths)
{
        Get-ChildItem -Path $path -File -Recurse | where { $_.LastWriteTime -lt $limit -and $_.FullName -contains "cdatalogs"} | Remove-Item
        Write-Host "Files from $path removed"
}

I don't get any error, but no file is deleted when I run the script.

Does anyone can help me, please?

Thank you!
Ney Santos


 



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>