Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Can I use PDQ Deploy to delete folders and files of a certain age?

I'd like to create a package that allows me to delete files and folders either by date range (e.g. 1/1/2005- 1/1/2015) or by x# of days (last modified or accessed, doesn't matter) to run when I want, or at a designated time/date.

In every iteration of package I've tried just as a test myself on my local PC (using CMD as the first step- rmdir /s /q/ \servername\dir) I'm met with Failed, error 2. Could simply be syntax, or whatever, but most importantly I'd like to ONLY remove files and folders of a certain age. Anyone ever try this or does it exist natively within PDQ Deploy or PDQ Inventory? Any help or suggestions is appreciated!



Date Votes
  • Hi Mike, Maybe the following can assist or point you in the right direction The "@(temp)" path is a custom variable I set in PDQ deploy to target C:\Temp on the target machine

    #Define Path
    $Path = “@(temp)”
    #Get Current Date
    $CurrentDate = Get-Date
    #Define age '50 days old'
    $Daysback = "-50”
    #Current date minus specified age
    $DatetoDelete = $CurrentDate.AddDays($Daysback)
    $FilesToDelete = Get-ChildItem $Path -Recurse -File | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Verbose
  • Many thanks! I'll give this a go... Best Mike