Last active
May 12, 2018 02:20
-
-
Save auberginehill/13bb9f56dc0882bf5e85a8f88ccd4610 to your computer and use it in GitHub Desktop.
A Windows PowerShell script for removing empty folders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Remove-EmptyFoldersLite.ps1 | |
#> | |
# $path = "." | |
$path = "C:\Temp" | |
$filename = "deleted_folders.txt" | |
$txt_file = "$env:temp\$filename" | |
$separator = "---------------------" | |
$empty_line = "" | |
$empty_folders = @() | |
$deleted_folders = @() | |
# Test if the path exists | |
If ((Test-Path $path) -eq $false) { | |
$invalid_path_was_found = $true | |
# Display an error message in console | |
$empty_line | Out-String | |
Write-Warning "'$path' doesn't seem to be a valid path name." | |
$empty_line | Out-String | |
Write-Verbose "Please consider checking that the starting point location '$path' was typed correctly and that it is a valid file system path, which points to a directory." -verbose | |
$empty_line | Out-String | |
$skip_text = "Couldn't open '$path'..." | |
Write-Output $skip_text | |
$empty_line | Out-String | |
Exit | |
} Else { | |
# Resolve path (if the path is specified as relative) | |
$real_path = Resolve-Path -Path $path | |
} # else (if) | |
# Search recursively for the empty folders at $real_path (i.e. $path) | |
# Note: For best results against nested empty folders, please run the script iteratively until no empty folders are found. | |
# Credit: Mekac: "Get folder where Access is denied" https://social.technet.microsoft.com/Forums/en-US/4d78bba6-084a-4a41-8d54-6dde2408535f/get-folder-where-access-is-denied?forum=winserverpowershell | |
$available_folders = Get-ChildItem $real_path -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer -eq $true } | Select-Object FullName, @{ Label="AclDenied"; Expression={ (Get-Acl $_.FullName).AreAccessRulesProtected }} | Where-Object { $_.AclDenied -eq $false } | Select-Object FullName | Sort FullName | |
$unavailable_folders = Get-ChildItem $real_path -Recurse -Force -ErrorAction SilentlyContinue | Where-Object { $_.PSIsContainer -eq $true } | Select-Object FullName, @{ Label="AclDenied"; Expression={ (Get-Acl $_.FullName).AreAccessRulesProtected }} | Where-Object { $_.AclDenied -eq $null } | Select-Object FullName | Sort FullName | |
ForEach ($folder in ($available_folders)) { | |
$the_query_of_empty_folders = Get-ItemProperty $folder.FullName | Where-Object { ($_.GetFiles().Count -eq 0) -and ($_.GetDirectories().Count -eq 0) } | Select-Object FullName | |
If ($the_query_of_empty_folders -ne $null) { | |
$empty_folders += New-Object -TypeName PSCustomObject -Property @{ | |
'FullName' = $folder.FullName | |
} # New-Object | |
} Else { | |
$continue = $true | |
} # Else (If $the_query_of_empty_folders) | |
} # ForEach $folder | |
If ($empty_folders.Count -eq 0) { | |
$empty_line | Out-String | |
"Didn't find any empty folders at $real_path." | Out-String | |
$empty_line | Out-String | |
Exit | |
} Else { | |
# Create a list of the empty folders and delete the empty folders | |
ForEach ($directory in $empty_folders) { | |
$deleted_folders += New-Object -TypeName PSCustomObject -Property @{ | |
'Deleted Empty Folders' = $directory.FullName | |
} # New-Object | |
Remove-Item $directory.FullName -Force | |
} # ForEach | |
# Write the deleted directory paths in console | |
$empty_line | Out-String | |
$deleted_folders.PSObject.TypeNames.Insert(0,"Deleted Empty Folders") | |
Write-Output $deleted_folders | |
$empty_line | Out-String | |
# Write the deleted directory paths to a TXT-file (located at the current temp-folder) | |
If ((Test-Path $txt_file) -eq $false) { | |
$deleted_folders | Out-File "$txt_file" -Encoding UTF8 -Force | |
Add-Content -Path "$txt_file" -Value "Date: $(Get-Date -Format g)" | |
} Else { | |
$pre_existing_content = Get-Content $txt_file | |
($pre_existing_content + $empty_line + $separator + $empty_line + ($deleted_folders | Select-Object -ExpandProperty 'Deleted Empty Folders') + $empty_line) | Out-File "$txt_file" -Encoding UTF8 -Force | |
Add-Content -Path "$txt_file" -Value "Date: $(Get-Date -Format g)" | |
} # Else (If Test-Path) | |
} # Else (If $empty_folders.Count) | |
# Source: https://gist.github.com/nedarb/840f9f0c9a2e6014d38f | |
# Source: https://gist.github.com/Appius/d863ada643c2ee615db9 | |
# Source: http://www.brangle.com/wordpress/2009/08/append-text-to-a-file-using-add-content-in-powershell/ | |
# Credit: Mekac: "Get folder where Access is denied" https://social.technet.microsoft.com/Forums/en-US/4d78bba6-084a-4a41-8d54-6dde2408535f/get-folder-where-access-is-denied?forum=winserverpowershell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[reserved]