简体   繁体   中英

Deleting users registry with batch

I recently started working at a school and we are doing our bi-yearly wiping of user accounts from all of the computers. We use persistent accounts for all of the students to reduce wear on the HDDs, reduce network load, and to reduce boot time. For deleting the user folders we use this script:

@echo off
cd \.
cd \Users\
dir *20**
echo Users to be deleted
pause
@echo on
for /D %%f in (*20**) do rmdir %%f /s /q
pause
exit

The only issue is when multiple grades use the same computer a student that had their profile downloaded to the computer the previous year isn't able to login as their registry is still present on the computer. How would I go about removing their registry entries (with batch if possible) that way there wouldn't be any registry conflicts?

All of our computers are running Windows 10 if that changes anything. TIA

Sharing experience about this kind of stuff: I'm kind of administating a pool of Windows XP machines, with domain accounts but local profiles.

We started with roaming profiles, but it was so hectic that we decided to switch to local profiles by default (by changing some system registry key). There was the question of destroying corrupt roaming user profiles so they will be created as local profiles afterwards.

In that case, removing everything in %USERPROFILE% for a given user removes the file NTUSER.DAT and thus all his/her settings/registry entries (but not the right to connect again)

After that, if the student connects with a non-existing profile, Windows creates a standard profile cloned from Default User profile.

This does not work if the machine has not been rebooted after user successful login: NTUSER.DAT subsists, but not the other files, which may be problematic, and maybe the problem that you're having.

What I'd do:

  • remote reboot of all machines to unlock NTUSER.DAT files
  • for all machines, remotely destroy the Users sub-dirs (you just need an extra for loop containing all your machines and access the drives like that: \\\\host\\C$\\Users (provided that Users is on C drive)

If you want to delete Registry entry of a certain student on a remote PC, save this script to test.bat , and run it from open Admin Cmd Prompt . Replace PC_name and user values with your own. You can use wildcards in the user name, ie "user=.*20.*" to delete Reg data of all users with name like AshleyPalm-2016 . It works well for me. Let me know if any errors pop up:

@echo off
setlocal enabledelayedexpansion
set "PC_name=remote_PC_name" & set "user=.*20.*" & echo/
for /f "usebackq tokens=*" %%H in (`reg query \\%PC_name%\HKEY_USERS`) do (
    set "val=\\%PC_name%\%%H\Volatile Environment"
    for /f "usebackq tokens=3 skip=1" %%K in (`reg query "!val!" /v USERNAME 2^>nul`) do (
        echo %%K | findstr /r /e /c:"%user%" >nul && (echo Deleting %%K: %%H & reg delete \\%PC_name%\%%H )))
exit /b

Read more here: Reg command (MS), FINDSTR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM