Help with simple script to remove subfolders from directory structure

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Vern Anderson Vern Anderson 2 years, 7 months ago.

  • Author
    Posts
  • #15093
    Profile photo of Buch0
    Buch0
    Participant

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

    \\server\share\UPMProfiles\username1\UPM_Profile\Downloads
    \\server\share\UPMProfiles\username2\UPM_Profile\Downloads
    \\server\share\UPMProfiles\username3\UPM_Profile\Downloads
    \\server\share\UPMProfiles\username4\UPM_Profile\Downloads

    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.

    Thanks

  • #15099
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    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
    Profile photo of Vern Anderson
    Vern Anderson
    Participant

    # 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

    -VERN

You must be logged in to reply to this topic.