Help with simple script to remove subfolders from directory structure

This topic contains 2 replies, has 3 voices, and was last updated by  Vern Anderson 4 years, 1 month ago.

  • Author
  • #15093


    I am trying to clear the 'Downloads' folder from all of our user profiles, the structure is:


    I can't figure out how to empty the contents of the 'Downloads' folder from user profiles without having a separate line for each different username..

    Batches like del "\\server\share\UPMProfiles\*\UPM_Profile\Downloads" don't work.

    Any suggestions would be greatly appreciated.


  • #15099

    Bob McCoy

    You could use the wildcard and pipe the results to Remove-Item.

    dir \\server1\c$\users\*\downloads\* | where LastWriteTime -lt $cutoff | rm -Confirm

    In this example, I removed files. But you could also remove whole directories. You will want to use the -Recurse and -Force switches.

  • #15111

    Vern Anderson

    # Remember to remove -Whatif parameter when you're ready to actually delete them
    Get-ChildItem \\server\share\UPMProfiles\*\UPM_Profile\Downloads\*.* | ForEach-Object {Remove-Item $_.Fullname -WhatIf}

    # but the trick you were missing is the Foreach-Object


You must be logged in to reply to this topic.