Cleanup User Download Folders in Profile Directories

run this powershell script on the file server which houses the Profile Directories.


$ROOTDIR = "<Relative path to Profiles DIR>"

$item = ( gci $ROOTDIR)

foreach ( $USERDIR in $item ) { gci "$ROOTDIR\$USERDIR\downloads" -file -recurse | remove-item }

First we need to create and ARRAY (=variable with multiple items, this we do by filling $item which the content of gci (get-childitem) = all of the directories under D:\Profiles)

What this script does is.. for EACH Directory in ITEM (Item is D:\Profiles\<username>) do a GCI (= alias for Get-ChildItem) for $ROOTDIR\USERDIR$\Downloads (=same as <Relative Profile Path><Username>\Downloads) and -file (show all files) and loop through all subfolders of DIR$\Downloads (-recurse).. and remove all files you find (remove-item).

Lots of thanks for this to a scripting Buddy of mine.



A different and maybe simpler one lines to get the same results is

gci <Full Path to Profiles DIR>\*\downloads -recurse | Select-Object FullName -ExpandProperty Fullname | Remove-Item -recurse -Confirm:$False


Alternatively you could add something in between to select files of a certain "Last Access" or/and "Last Write" time with

where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } 

When you combine the two it would look something like

gci <Full Path to Profiles DIR>\*\downloads -recurse | where-object { $_.LastAccessTime -lt (Get-Date).AddDays(-14) -and $_.LastWriteTime -lt (Get-Date).AddDays(-14) } | Select-Object FullName -ExpandProperty Fullname | Remove-Item -recurse -Confirm:$False