Powershell: Recursive delete of Mac network homes caches folders on windows server overcoming "The specified path, file name, or both are too long."


$path = "E:\FacultyHome1"
 
$items = Get-ChildItem -Path $path
 
foreach ($item in $items) {
  # if the item is a directory, then process it.
  if ($item.Attributes -eq "Directory") {
    #echo $item.Name
    $subpath = $path + "\" + $item.Name + "\Library"
    #echo $subpath 
    $subitems = Get-ChildItem -Path $subpath
    foreach ($subitem in $subitems) {
      # if the item is a directory, then process it.
      if ($subitem.Attributes -eq "Directory" -and $subitem.Name -eq "Caches") {
        echo $item.Name
	$pathToCaches = $subpath + "\" + $subitem.Name
	echo $pathToCaches
	robocopy c:\tmp $pathToCaches /s /mir /NFL /NDL /NJH /NJS /nc /np  /ns
	remove-item -force $pathToCaches -recurse
      }
    }
  }
}
© 2011 Adam Gerson.