Remove-EmptyFolders (Powershell function)

This function will remove all folders that are completely empty.  If a folder has no files, but has sub-folders with files, the parent folder will not be deleted.  Only completely empty folder chains will be deleted.

2,459 times
Add to favorites
E-mail Twitter Digg Facebook
  • Script dialog order update
    1 Posts | Last post October 14, 2013
    • Hi Bigteddy,
      I ran this script against a large list of folders to be cleaned up, and noticed that the instructions rolled off the buffer. So, for usability, I edited my local copy so that line 34 reads: Write-Host "# # # The following empty directories will be removed # # #" and moved line 35 - "# Write-Host "Press 'y' to continue, any other key to stop:" to line 37, right after $delDirs command. This way, the script lists out all the directories to be deleted, then prompts the user for next actions, instead of appearing halted after enumerating the last empty folder.
  • Basic PowerShell Question
    1 Posts | Last post May 14, 2013
    • Hi,
      My question is so basic I have not been able to find the answer. When running the script does the script file have to be in the parent folder? That is what I did and it appears to have worked correctly. I am asking for future reference.
      Thanks for you help!
  • Any reason why this wouldn't run on Windows 8?
    1 Posts | Last post April 13, 2013
    • Hi
      Thanks for the script, but struggling to get it to work on Windows 8.
      I've set the execution policy to unsigned (for the moment...), and when I try and run it I get the confirmation message ([D] Do not run [R] Run once etc.), but after entering R nothing happens. Tried to call it in every way I can think of but it just doesn't seem to do anything.
      Calling from normal PowerShell prompt as Administrator.
  • Folder Names with Square Brackets
    1 Posts | Last post January 30, 2013
    • Hi:  This script does not work properly with folders having square brackets in them such as "C:\music\Greatest Hits [Disc 1]".  This appears to be due to the special use of brackets for defining ranges by Get-ChildItem.  But when I use -LiteralPath that does not work with -Recurse.  What do you think? - Thanks
  • ReadKey error
    2 Posts | Last post October 30, 2012
    • I'm getting the following error. I'm unable to get past it.
      Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
      At C:\Scripts\Remove-EmptyFolders.ps1:30 char:9
      +     if ($host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown").Character -eq 'y') {
      +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : NotImplementedException
    • This will happen if you run the script from the ISE.  Run it from a normal Powershell prompt.
  • VBscript
    2 Posts | Last post June 08, 2012
    • Hello,
      Can you convert your script to VBscript?
      I find it a very interesting script but i need it in VBscript or for my finals
      I would really appreciate it if you could convert it.
      Or do you know someone who can convert it to Vbscript or
    • I'm afraid not.  I only do Powershell!