Script to delete folders

Phantom410

Honorable
Nov 5, 2013
68
0
10,660
I am looking for a script that does the following:

delete all folders that starts with 123

the folders are located in the c:\users

I have tried and here is what I came up with:

Remove-Item c:\users\* -include *123

Any suggestions from the pros?
 
Solution
Put this in a cmd file, note that this isnt recursive, i.e. c:\users\XYZ\123a will not be deleted, but c:\users\123a will)

SET BASEFOLDER=C:\users
FOR /F %%i in ('dir /ad /b %BASEFOLDER%\123*') do (
rmdir /s /q "%BASEFOLDER%\%%i"
)

if you need recursive support you can use /s in the dir command, but change the rmdir line to rmdir /s /q "%%i"
Put this in a cmd file, note that this isnt recursive, i.e. c:\users\XYZ\123a will not be deleted, but c:\users\123a will)

SET BASEFOLDER=C:\users
FOR /F %%i in ('dir /ad /b %BASEFOLDER%\123*') do (
rmdir /s /q "%BASEFOLDER%\%%i"
)

if you need recursive support you can use /s in the dir command, but change the rmdir line to rmdir /s /q "%%i"
 
Solution


Thank You so much!