-1

I'm trying to write a batch file to delete all folders within c:\users with some exceptions.

Scenario: We have multiple shared PCs in a library which users may log onto and never use again. This often fills up the c:\ drive with user profiles.

I would like to remove all folders from c:\users except c:\users\defaultuser0 and c:\users\public.

Hopefully that makes sense? I've seen some other questions/answers but they only specify how to specify 1 folder. Also, I am aware of delprof as we do use it in other applications but I would like to write my own file so I can amend it for this case.

Compo
  • 36,585
  • 5
  • 27
  • 39
Synergeh
  • 1
  • 1
  • 1
  • Get started here: https://stackoverflow.com/questions/16078421/how-do-i-get-a-list-of-folders-and-sub-folders-without-the-files – JJFord3 Feb 22 '19 at 15:37
  • 3
    Simply removing directories is not the correct way to perform this task. There will still be information about those profiles held in the system, so whilst you may free up HDD space you will still have many, now invalid, references to locations which no longer exist. You mentioned in your question the old DelProf utility, and may, unless you're using Windows 10, prefer the unofficial [DelProf2](https://helgeklein.com/free-tools/delprof2-user-profile-deletion-tool) utility, which is scriptable and ignores special profiles and has options for excluding named profiles too. – Compo Feb 22 '19 at 15:57

4 Answers4

2

If you still wanted to delete the directories, against my recommendation, I'd suggest using WMIC like this, from a batch file:

@For /F "Tokens=1*Delims==" %%A In ('WMIC Path Win32_UserProfile Where "Special!='True' And LocalPath Is Not Null And Not LocalPath Like '%%\\defaultuser0'" Get LocalPath /Value 2^>Nul')Do @For /F "Tokens=*" %%C In ("%%B")Do @RD/S/Q "%%C"

If your preference was for the Net User method, I'd still suggest WMIC from a batch file, like this:

@For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
 "Special!='True' And LocalPath Is Not Null" Assoc^
 /AssocClass:Win32_UserAccount 2^>Nul')Do @For /F "Tokens=1*Delims==" %%B In ('
    WMIC UserAccount Where^
 "SID='%%A' And LocalAccount='TRUE' And Name!='defaultuser0'" Get Name /Value^
 2^>Nul')Do @For /F Tokens^=* %%D In ("%%C")Do @Net User "%%D" /Delete

Using WMI to retrieve the user account paths and/or names is much more robust that just selecting directories by default location and users original names

Compo
  • 36,585
  • 5
  • 27
  • 39
0

You seem to misunderstand what is a userprofile and what users are. Even if you remove their folder from C:\Users\username, still some info will be kept as mentioned by Compo here, e.g. in some programs you have installed in C:\Program Files*\*, in C:\Windows\*, e.t.c. The info won't be lost if you just remove the userprofile directory.

However, to achieve what you want, although not recommended for messing up your system, use (run with admin privileges):

@echo off
setlocal EnableDelayedExpansion

set exclude_list="defaultuser0" "public"

for /F "delims= eol=" %%A IN ('dir /B /AD "C:\Users\"') do (
    if "!exclude_list:%%A=!" == "%exclude_list%" (rd /s /q "%%~fA")
)

echo Cleared the unneeded folders in C:\Users^^! Folders remaining:
dir /b "C:\Users\"

The command to delete a user is officially in cmd net user username /delete. Do the following to do so:

@echo off
setlocal EnableDelayedExpansion

set exclude_list="defaultuser0" "public"

for /F "delims= eol=" %%A IN ('dir /B /AD "C:\Users\"') do (
    if "!exclude_list:%%A=!" == "%exclude_list%" (net user %%~nxA /delete)
)

Note that in both cases add quoted a foldername you want to exclude.

For more information about the commands used, try taking a look at the help pages of the following commands in cmd:

  • echo /?
  • setlocal /? enabled because we can't do %exclude:%%A=%. Too many %.
  • set /?
  • for /?
  • if /?
  • rd /?
  • net user /?

Some interesting link references for further reading:

double-beep
  • 5,031
  • 17
  • 33
  • 41
0

TL;DR:

wmic /namespace:\\root\cimv2 path win32_userprofile where "SID LIKE 'S-1-5-21%' AND NOT LocalPath LIKE '%-Admin' AND NOT LocalPath LIKE '%\\ToolBox'" delete

Details:

In my head, I translated the question from "delete all folders" and instead made it "remove all user profiles". If that's wrong, feel free to beat me. :-)

I recently had a project where I needed to do the same thing and I used the solution above. The one caveat is that Windows will not remove a profile that is active and currently loaded in memory - and I considered this to be a good thing. In those cases, once the system reboots, the profile is no longer loaded and the 'DELETE' call will work fine. This will not just remove the directories and files, but it will also perform typical profile cleanup work as well.

Please be aware - I know it's redundant, but as this removes profiles, that means files kept in profiles can/will be removed as well. You may wish to backup the files before being deleted.

Look and Report

wmic /namespace:\\root\cimv2 path win32_userprofile where "SID LIKE 'S-1-5-21%' AND NOT LocalPath LIKE '%-Admin' AND NOT LocalPath LIKE '%\\ToolBox'" get LocalPath,LastUseTime,SID

Seek and Destroy

wmic /namespace:\\root\cimv2 path win32_userprofile where "SID LIKE 'S-1-5-21%' AND NOT LocalPath LIKE '%-Admin' AND NOT LocalPath LIKE '%\\ToolBox'" delete
Community
  • 1
  • 1
-1

You can backup the folders to keep, remove all folders then finally move the backup folders back to the location, here is an example:

@echo off
md c:\folderbackup
copy c:\users\defaultuser0 c:\folderbackup\defaultuser0
copy c:\users\public c:\folderbackup\public
del /f /q c:\users\*.*
move c:\folderbackup\defaultuser0 c:\users\defaultuser0
move c:\folderbackup\public c:\users\public
rd c:\folderbackup

If it says Access Denied then try running as administrator.

Hayz
  • 176
  • 4