Deleting All Subfolders Below A Given Level

by Nov 16, 2018

Here is another file system task that sounds worse than it actually is. Let’s say you need to remove all folders below a given level in a folder structure. Here is how:

# Task:

# remove all folders one level below the given path
Get-ChildItem -Path "c:\sample\*\" -Directory -Recurse | 
    # remove -WhatIf to actually delete
    # ATTENTION: test thoroughly before doing this!
    # you may want to add -Force to Remove-Item to forcefully delete
    Remove-Item -Recurse -WhatIf

Simply use “*” in your path. To delete all folders two levels below a given path, adjust accordingly:

# Task:

# remove all folders TWO levels below the given path
Get-ChildItem -Path "c:\sample\*\*\" -Directory -Recurse | 
    # remove -WhatIf to actually delete
    # ATTENTION: test thoroughly before doing this!
    # you may want to add -Force to Remove-Item to forcefully delete
    Remove-Item -Recurse -WhatIf

Twitter This Tip! ReTweet this Tip!