Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Param (
  2.     [string]$Source = "C:\Temp",
  3.     [string]$Days = "1"
  4. )
  5.  
  6.  
  7. $Files = Get-ChildItem $Source -Recurse | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).addminutes(-$($Days)) }
  8.  
  9. $Files | Remove-Item -Force
  10.  
  11. if ($Files.count -gt 0)
  12. {
  13.     $Folders = @()
  14.     ForEach ($Folder in (Get-ChildItem -Path $Source -Recurse -Directory))
  15.     {
  16.         $Folders += New-Object PSObject -Property @{
  17.             Object = $Folder
  18.             Depth = ($Folder.FullName.Split("\")).Count
  19.         }
  20.     }
  21.  
  22.     $Folders = $Folders | Sort Depth -Descending
  23.     ForEach ($Folder in $Folders)
  24.     {  
  25.         If ($Folder.Object.GetFileSystemInfos().Count -eq 0)
  26.         {
  27.             Write-Host "Removing Folder: $($Folder)"
  28.             Remove-Item -Path $Folder.Object.FullName -Force
  29.         }
  30.     }
  31. }
  32. else
  33. {
  34.     Write-Host "No Empty folders found after removing files older than $($Days) days."
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement