Help with simple script to remove subfolders from directory structure

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

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.

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