locked
Delete downloaded files from roaming profiles older than 30 days RRS feed

  • Question

  • Hello everybody,

    On my server I have a folder that stores all the roaming profiles. These folders also include the 'Downloads' folder.

    I would like some help generating a script (cmd or powershell) that lists all Downloads folder of every user and deletes all files older than 30 days.

    D:\Profiles\User-A\Downloads\
    D:\Profiles\User-B\Downloads\
    ..
    .

    I found the code below on this site. It works, but I need some small adjustment and I can't figure it out.
    The code should only parse the folder D:\Profiles recursively. It starts from D:\ instead.

    $Folders = Get-ChildItem D:\Profiles -Recurse | Where-Object {($_.PSIsContainer) -AND ($_.Name -EQ "Downloads")}
    ForEach ($Folder in $Folders)
    	{
    	Get-ChildItem $Folder -Recurse | Where-Object {$_.LastWriteTime -LT (Get-Date).AddDays(-30)} | ForEach-Object {Remove-Item $_.Fullname -Force}
        }

    Kind regards,

    John

    Wednesday, October 4, 2017 12:57 PM

Answers

  • It does work.  Have you tried it?  It will search recursively for any folder named "Downloads" anywhere in the hierarchy.


    \_(ツ)_/

    • Marked as answer by John Blom Wednesday, October 4, 2017 3:16 PM
    Wednesday, October 4, 2017 2:15 PM
  • Get-ChildItemD:\Profiles\Downloads -Recurse -Directory

    \_(ツ)_/

    • Marked as answer by John Blom Wednesday, October 4, 2017 3:16 PM
    Wednesday, October 4, 2017 1:11 PM

All replies

  • Get-ChildItemD:\Profiles\Downloads -Recurse -Directory

    \_(ツ)_/

    • Marked as answer by John Blom Wednesday, October 4, 2017 3:16 PM
    Wednesday, October 4, 2017 1:11 PM
  • Thnx, for your reply. That won't work. The folder D:\Profiles contains user profile folders and in each of those folders you will find a Downloads folder.

    I want to delete all files from D:\Profiles\%variable%\Downloads.

    Kind regards,

    John

    Wednesday, October 4, 2017 1:49 PM
  • It does work.  Have you tried it?  It will search recursively for any folder named "Downloads" anywhere in the hierarchy.


    \_(ツ)_/

    • Marked as answer by John Blom Wednesday, October 4, 2017 3:16 PM
    Wednesday, October 4, 2017 2:15 PM
  • Sorry! My bad!! I forgot to change the date of my test files....again. :-(

    It works perfectly. Many thnx!

    Kind regards,

    John

    Wednesday, October 4, 2017 3:16 PM
  • no a powershell solution, but use this it works very fine and simple with one line

    https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool/


    Chris

    Thursday, October 5, 2017 5:01 PM
  • Hi Chris,

    Nice solution. Thnx.

    Regards,

    John

    Monday, October 9, 2017 8:19 AM