How to delete subfolders and Files but not parent Folder in windows using Script? [closed]

As PowerShell supports wildcards/patterns on multiple levels of a path,
it’s as simple as:

Get-ChildItem D:\test[1-3]\* | Remove-Item -Recurse -Force

Sample tree before and after running the command:

> tree /F
D:.
├───test1
│   └───test1
│       └───archive
│               x.txt
│
├───test2
│   └───try
│       └───archive
│               x.txt
│
└───test3
    └───model
        └───archive
                x.txt

> Get-ChildItem D:\test[1-3]\*|Remove-Item -Recurse -Force
> tree /f
D:.
├───test1
├───test2
└───test3

Leave a Comment