Solved Find & delete empty folders in path specified (with folder path display & confirmation) using Powershell


antspants

Like a rolling drone.
Guru
VIP
Local time
9:59 PM
Posts
14,870
Location
Gold Coast, Australia
OS
Windows 11 Pro 23H2 Build 22631.5624

Tested before posting:


  • Before running this script, change the second line
    • $drive = "Y:\" where Y:\ is the drive in the script, change to the specific drive (or path) you are wanting to search and destroy empty folders on. So for example, change to "G:\" $drive = "G:\"
I tested with $drive = "Y:\Test Path\" and all went nicely.​
(image of results below.)​
  • The script will search for and find each empty folder on a drive or in a path specified (one at a time)
  • Added the function to display the folder path and ask for confirmation before deleting (y/n)
⚠️ Do NOT run this for the root of C: Removing empty folders from your system drive could be detrimental.
⚠️ Change path to User Folders or external drives only

To Run:

  • Open Terminal (Admin)
    • (Right click start button to select "Terminal (Admin)". Or use search & search for “Powershell” and right click to open as admin)

  • Copy and paste the following script into the Powershell window and press enter.


Powershell:
# Set the drive to search
$drive = "Y:\"

# Function to find and delete empty folders
function Delete-EmptyFolders {
    param (
        [string]$path
    )

    # Get all directories recursively
    $folders = Get-ChildItem -Path $path -Recurse -Directory | Sort-Object -Property FullName -Descending

    foreach ($folder in $folders) {
        # Check if the folder is empty
        if (-not (Get-ChildItem -Path $folder.FullName -Force)) {
            Write-Host "Empty folder found: $($folder.FullName)"
            $confirm = Read-Host "Do you want to delete this folder? (y/n)"
            if ($confirm -eq 'y') {
                Remove-Item -Path $folder.FullName -Force
                Write-Host "Folder deleted: $($folder.FullName)"
            } else {
                Write-Host "Folder not deleted: $($folder.FullName)"
            }
        }
    }
}

# Run the function
Delete-EmptyFolders -path $drive

pause


Results:

IMG_5009.png

I hope it is useful to you.
 

Attachments

Last edited:

My Computers

System One System Two

  • OS
    Windows 11 Pro 23H2 Build 22631.5624
    Computer type
    PC/Desktop
    Manufacturer/Model
    Sin-built
    CPU
    Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz (4th Gen?)
    Motherboard
    ASUS ROG Maximus VI Formula
    Memory
    32.0 GB of I forget and the box is in storage.
    Graphics Card(s)
    Gigabyte nVidia GeForce GTX 1660 Super OC 6GB
    Sound Card
    Onboard
    Monitor(s) Displays
    5 x LG 25MS500-B - 1 x 24MK430H-B - 1 x Wacom Pro 22" Tablet
    Screen Resolution
    All over the place
    Hard Drives
    Too many to list.
    OS on Samsung 1TB 870 QVO SATA
    PSU
    Silverstone 1500
    Case
    NZXT Phantom 820 Full-Tower Case
    Cooling
    Noctua NH-D15 Elite Class Dual Tower CPU Cooler / 6 x EziDIY 120mm / 2 x Corsair 140mm somethings / 1 x 140mm Thermaltake something / 2 x 200mm Corsair.
    Keyboard
    Corsair K95 / Logitech diNovo Edge Wireless
    Mouse
    Logitech: G402 / G502 / Mx Masters / MX Air Cordless
    Internet Speed
    1000/400Mbps
    Browser
    All sorts
    Antivirus
    Kaspersky Premium
    Other Info
    I’m on a horse.
  • Operating System
    Windows 11 Pro 23H2 Build: 22631.4249
    Computer type
    Laptop
    Manufacturer/Model
    LENOVO Yoga 7i EVO OLED 14" Touchscreen i5 12 Core 16GB/512GB
    CPU
    Intel Core 12th Gen i5-1240P Processor (1.7 - 4.4GHz)
    Memory
    16GB LPDDR5 RAM
    Graphics card(s)
    Intel Iris Xe Graphics Processor
    Sound Card
    Optimized with Dolby Atmos®
    Screen Resolution
    QHD 2880 x 1800 OLED
    Hard Drives
    M.2 512GB
    Antivirus
    Defender / Malwarebytes
    Other Info
    …still on a horse.
Also, from @Brink

Add "Find and Delete Empty Folders" context menu in Windows 11 and Windows 10​


 

My Computers

System One System Two

  • OS
    Windows 11 Pro 23H2 Build 22631.5624
    Computer type
    PC/Desktop
    Manufacturer/Model
    Sin-built
    CPU
    Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz (4th Gen?)
    Motherboard
    ASUS ROG Maximus VI Formula
    Memory
    32.0 GB of I forget and the box is in storage.
    Graphics Card(s)
    Gigabyte nVidia GeForce GTX 1660 Super OC 6GB
    Sound Card
    Onboard
    Monitor(s) Displays
    5 x LG 25MS500-B - 1 x 24MK430H-B - 1 x Wacom Pro 22" Tablet
    Screen Resolution
    All over the place
    Hard Drives
    Too many to list.
    OS on Samsung 1TB 870 QVO SATA
    PSU
    Silverstone 1500
    Case
    NZXT Phantom 820 Full-Tower Case
    Cooling
    Noctua NH-D15 Elite Class Dual Tower CPU Cooler / 6 x EziDIY 120mm / 2 x Corsair 140mm somethings / 1 x 140mm Thermaltake something / 2 x 200mm Corsair.
    Keyboard
    Corsair K95 / Logitech diNovo Edge Wireless
    Mouse
    Logitech: G402 / G502 / Mx Masters / MX Air Cordless
    Internet Speed
    1000/400Mbps
    Browser
    All sorts
    Antivirus
    Kaspersky Premium
    Other Info
    I’m on a horse.
  • Operating System
    Windows 11 Pro 23H2 Build: 22631.4249
    Computer type
    Laptop
    Manufacturer/Model
    LENOVO Yoga 7i EVO OLED 14" Touchscreen i5 12 Core 16GB/512GB
    CPU
    Intel Core 12th Gen i5-1240P Processor (1.7 - 4.4GHz)
    Memory
    16GB LPDDR5 RAM
    Graphics card(s)
    Intel Iris Xe Graphics Processor
    Sound Card
    Optimized with Dolby Atmos®
    Screen Resolution
    QHD 2880 x 1800 OLED
    Hard Drives
    M.2 512GB
    Antivirus
    Defender / Malwarebytes
    Other Info
    …still on a horse.

Latest Support Threads

Back
Top Bottom