Skip to content

Instantly share code, notes, and snippets.

@Greyeye
Last active December 11, 2015 01:59
Show Gist options
  • Save Greyeye/4527657 to your computer and use it in GitHub Desktop.
Save Greyeye/4527657 to your computer and use it in GitHub Desktop.
# My UPM's "Path To User Store" value in GPO is
# \\DFSNameSpace\Profiles$\xa6\#company#\#samAccountName#
# result can be like, \\DFSNameSpace\Profiles$\xa6\XYZ Co\Tom.John
# so this script will loop twice (one for list of companies, other for list of users)
#
# This is because I work for Citrix Service Provider and system is multi-tennanted.
# if you only have 1 company, you can remove the for loop at line 34
# most important line is at line 48, this line controls what you target and how its filtered.
# foreach ($file in get-childitem $userpath -recurse -force | where {$_.PSIsContainer -eq $false -and $_.lastwritetime -le $datefilter -and $_.name -ne "desktop.ini"})
#change the number to filter the files OLDER than dates, eg, 90 means select files older than 90
$days = 90
#change silent to $true, if you want to see the remote-item in progress.
$silent = $false
#change profile location
$profileLocation = "E:\Profiles\XA6"
$now=get-date
#to target recent items change path below to
# recent = \UPM_Profile\AppData\Roaming\Microsoft\Windows\Recent
# cookies = \UPM_Profile\AppData\Roaming\Microsoft\Windows\Cookies
$targetPath = "\UPM_Profile\AppData\Roaming\Microsoft\Windows\Cookies"
$totalFiles = 0
if ($silent){
$ea="Silentlycontinue"
} else {$ea="Continue"}
#get company lists, loop in for-each
foreach($compname in (Get-ChildItem -path $profileLocation))
{
$comppath = "$profileLocation\$compname"
#get usersnames under company (eg e:\profiles\xa6\ABC co.\james.hong)
foreach($username in (Get-ChildItem -path $comppath))
{
#once usernames are obtained, build user path to target cookies.
$userpath = $comppath+"\"+$username+$targetPath
#build date filter, $days is specified at the top.
$datefilter=$now.adddays(-$days)
#select all files older than specified date $days, but will not select desktop.ini and folder.
foreach ($file in get-childitem $userpath -recurse -force | where {$_.PSIsContainer -eq $false -and $_.lastwritetime -le $datefilter -and $_.name -ne "desktop.ini"})
{
#write-host "$userpath\$file"
$totalFiles = $totalFiles +1
write-host $totalFiles
#remove-item -literalPath $file.fullname -force -ea $ea
}#end for
}
}
write-host "total $totalfiles have been removed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment