SHARE
TWEET

Profile Cleanup

david62277 Nov 24th, 2015 (edited) 560 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #requires -version 2
  2. <#
  3. Written by David Ott
  4. Read and understand everything - key items are marked with ##### (5 hash tags) - these are items you have to edit, or just things to pay
  5. attention to.  Do not use this script in production until it has been tested!
  6. I wrote this script to clean up Citrix Profile Management Stores.  The script will clean up old items in domstore/recent, remove oice_15*
  7. junk folders from appdata, and remove exclusions from the profile (based on a .csv file)
  8.  
  9. It requres powershell v2 or above, an account with at least modify
  10. rights to the profile store (so it can delete things), and a .csv file with exclusions/sync directories/folders ($excludes variable)
  11. CSV format:
  12. The first row = headers = do not change these
  13. $Recycle.Bin, below is an example of an excluded folder with nothing to sync below it
  14. AppData\Roaming\Microsoft\Excel,"XLSTART,Excel*.xlb" below is an example of a directory which is excluded but items under it are synced
  15.  
  16. It gets a little tricky when you have an excluded folder such as
  17. Appdata\LocalLow
  18. but have directories\files multiple levels down that need to sync like
  19. Appdata\LocalLow\Google\GoogleEarth\*.kml
  20. Appdata\LocalLow\Sun\Java\Deployment\ext
  21. Appdata\LocalLow\Sun\Java\Deployment\security
  22. Appdata\LocalLow\Sun\Java\Deployment\deployment.properties
  23.  
  24. you have to break it up like below
  25.  
  26. ### example below ###
  27. Folder,Exclude
  28. $Recycle.Bin,
  29. AppData\Roaming\Microsoft\Excel,"XLSTART,Excel*.xlb"
  30. AppData\LocalLow,"Google,Sun"
  31. AppData\LocalLow\Google,GoogleEarth
  32. AppData\LocalLow\Google\GoogleEarth,*.kml
  33. AppData\LocalLow\Sun,Java
  34. AppData\LocalLow\Sun\Java,Deployment
  35. AppData\LocalLow\Sun\Java\Deployment,"ext,security,deployment.properties"
  36.  
  37. Last thing - this script will run silent unless something throws an error for some reason
  38. if you want to see the deletions happen look for #-verbose throughout the script and delete the #
  39. #>
  40. <##### define what you concider to be old files in domstore and recent #####>
  41. <##### ie: if you want it to be anything older than 3 months - change the -1 to -3 #####>
  42. $old = [datetime]::Today.AddMonths(-1)
  43.  
  44. <#### This is the .csv file you created that holds your excluded/synchronized dirs/files #####>
  45. $excludes = Import-Csv C:\Excludes.csv
  46.  
  47. <##### change "\\server\share" to the root of your profile store #####>
  48. $profiles = (gci \\server\share | ?{$_.psiscontainer -eq $true}).FullName | sort
  49.  
  50. <##### adjust this path portion to match your environment - in my environment under each profile there is a folder
  51. v2x64 and then under that is where UPM_Profile resides.  If in your environment it is any different set that path in the
  52. variable below - ie: if your path under each profile is just UPM_Profile - adjust $profpath to equal "UPM_Profile" - just
  53. make sure there is no leading or trailing "\" #####>
  54. $profpath = "v2x64\UPM_Profile"
  55.  
  56. ##### here is where the magic starts to happen
  57. foreach ($profile in $profiles) {
  58. ##### sets profile root - ie: \\server\share\user\UPM_Profile
  59. $profroot = Join-Path $profile $profpath
  60. ##### sets the domstore path
  61. $domstore = Join-Path $profroot "\AppData\Local\Microsoft\Internet Explorer\DOMStore"
  62. ##### sets the recent path
  63. $recent = Join-Path $profroot "AppData\Roaming\Microsoft\Windows\Recent"
  64. ##### sets the appdata path
  65. $appd = Join-Path $profroot "AppData"
  66.  
  67. <##### looks in appdata (if it exists) for any oice_15* folders (you may need to adjust this depending on the version of office) and then for each
  68. one it removes the directory #####>
  69. if (Test-Path $appd) {
  70. gci $appd | ?{($_.psiscontainer -eq $true) -and ($_.name -like "oice_15*")} | select -expand fullname | %{
  71. ri $_ -Recurse -Force #-Verbose
  72. }
  73. }
  74.  
  75. <##### this part cleans domstore of files that are older than the $old variable set at the beginning of this script #####>
  76. if (Test-Path $domstore) {
  77. <##### if the domstore directory exists - gets a list of all files (not folders) which were last written before $old excluding
  78. container.dat #####>
  79. $files = gci $domstore -Recurse -Force | ?{$_.psiscontainer -eq $false -and $_.LastWriteTime -le $old -and $_.Name -ne "container.dat"}
  80. <##### if there are files older than $old - delete them #####>
  81. if (($files | Measure-Object).count -gt "0") {
  82. $files | ri -Force #-Verbose
  83. }
  84. <##### if there are folders under domstore - checks to see if any of them are empty - if so deletes the empty folders #####>
  85. if ((gci $domstore -Force | ?{$_.psiscontainer -eq $true} | Measure-Object).count -ge "1"){
  86. (gci $domstore -Force | ?{$_.psiscontainer -eq $true}).FullName | %{
  87. if ((gci $_ -Recurse -Force | ?{$_.psiscontainer -eq $false} | Measure-Object).count -eq "0") {
  88. ri $_ -Recurse -Force #-Verbose
  89. }
  90. }
  91. }
  92. }
  93.  
  94. <##### cleans out recent of old shortcuts (older than $old) if the recent folder exists #####>
  95. if (Test-Path $recent) {
  96. <##### tries to get a list of all .lnk files older than $old - if it cannot (path too long) it will map a drive (Y:\) in order to shorten
  97. the path - if Y: is used on your system select a different drive letter - ctrl+h and replace any Y" with your drive letter followed by ":"
  98. as this script will map a drive as needed in a couple places
  99. ie: X: #####>
  100. try {
  101. $rfiles = gci $recent -Force -ErrorAction stop | ?{$_.psiscontainer -eq $false -and $_.LastWriteTime -le $old -and $_.Name -like "*.lnk"}
  102. } catch {
  103. <##### if the above command fails - maps a drive to shorten the path and gets a list of .lnk files #####>
  104. & net use Y: $recent
  105. Start-Sleep -Milliseconds 500
  106. $rfiles = gci Y:\ -Force | ?{$_.psiscontainer -eq $false -and $_.LastWriteTime -le $old -and $_.Name -like "*.lnk"}
  107. }
  108. <##### if any files are found older than $old - deletes them #####>
  109. if (($rfiles | Measure-Object).count -gt "0") {
  110. $rfiles | ri -Force #-Verbose
  111. }
  112. <##### If the drive was mapped - unmaps the drive #####>
  113. if (Test-Path "Y:\") {
  114. & net use Y: /d /y
  115. }
  116. }
  117.  
  118. <##### processes excluded folders from the .csv file #####>
  119. foreach ($exclude in $excludes) {
  120. <##### adds the excluded folder path to the profile path #####>
  121. $folder = join-path $profroot $exclude.Folder
  122. <##### checks to see if there are any exclusions to the excluded folder (sync directories/files - yeah I should have named it better)
  123. if there are it sets them as the $exclusions variable #####>
  124. if ($exclude.Exclude.Length -gt "0") {
  125. $exclusions = $exclude.Exclude -split ","
  126. } else {
  127. $exclusions = $null
  128. }
  129.  
  130. <##### If the excluded folder exists and there are items under it that should sync #####>
  131. if ((Test-Path $folder) -and ($exclusions -ne $null)) {
  132. <##### tries to get items under the folder - excluding sync items - and remove them #####>
  133. try {
  134. gci $folder -Force -Exclude $exclusions -ErrorAction Stop | ri -Recurse -Force -ErrorAction Stop #-Verbose
  135. } catch {
  136. <##### if the path is too long map a drive to shorten the path - get everything other than sync items and delete them - when done
  137. remove the network drive #####>
  138. & net use Y: (split-path $folder -Parent)
  139. gci (join-path "Y:\" (Split-Path $folder -Leaf)) -Force -Exclude $exclusions | ri -Recurse -Force #-Verbose
  140. & net use Y: /d /y
  141. }
  142. <##### else if there is an excluded folder and there are no items to worry about syncing below #####>
  143. } elseif (Test-Path $folder) {
  144. <##### tries to remove the folder and everything below it #####>
  145. try {
  146. ri $folder -Recurse -Force -ErrorAction Stop #-Verbose
  147. } catch {
  148. <##### if that fails - map a drive - then remove everything - and delete the network drive #####>
  149. & net use Y: (Split-Path $folder -parent)
  150. ri (Join-Path "Y:\" (Split-Path $folder -Leaf)) -Recurse -Force
  151. & net use Y: /d /y
  152. }
  153. }
  154. }
  155.  
  156. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top