Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Param (
- [string]$Source = "C:\Temp",
- [string]$Days = "1"
- )
- $Files = Get-ChildItem $Source -Recurse | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).addminutes(-$($Days)) }
- $Files | Remove-Item -Force
- if ($Files.count -gt 0)
- {
- $Folders = @()
- ForEach ($Folder in (Get-ChildItem -Path $Source -Recurse -Directory))
- {
- $Folders += New-Object PSObject -Property @{
- Object = $Folder
- Depth = ($Folder.FullName.Split("\")).Count
- }
- }
- $Folders = $Folders | Sort Depth -Descending
- ForEach ($Folder in $Folders)
- {
- If ($Folder.Object.GetFileSystemInfos().Count -eq 0)
- {
- Write-Host "Removing Folder: $($Folder)"
- Remove-Item -Path $Folder.Object.FullName -Force
- }
- }
- }
- else
- {
- Write-Host "No Empty folders found after removing files older than $($Days) days."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement