LeightonJames Posted October 8, 2013 Posted October 8, 2013 Ok....... I am trying to automate the removal of user profiles on Windows 7 and we have found that DelProf2 is coming up short for us. I have identified a way that we can do it through modifying the Registry at the following location: HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\ I have managed to identify which SID's need deleting based on the first 41 characters of the SID. e.g: S-1-5-21-1538596919-2946942214-34271393- What I'm looking to do is automate the polling of the registry and then remove the keys that match the above partial SID and then delete the corresponding folder within C:\Users (the folder path is referenced in the key as "ProfileImagePath") Has anyone got any suggestions?
SYNACK Posted October 8, 2013 Posted October 8, 2013 Powershell is going to be the best tool for this, it can mess with the registry like it is part of the file system.
LeightonJames Posted October 8, 2013 Author Posted October 8, 2013 I've never used Powershell but I had a feeling it might be the winner in this situation.
SYNACK Posted October 8, 2013 Posted October 8, 2013 I used something similar to kill a bad install of flash player (like they are not all bad) but it proved to be a much better tool for killing the keys than anything else.
FN-GM Posted October 8, 2013 Posted October 8, 2013 That batch file will do it @ECHO OFF :: -------------------------------------- :: :: Windows 7 User Profile Cleaning Script :: Version 3.1 :: :: Written by Mike Stone :: [email protected] :: http://mstoneblog.wordpress.com :: :: -------------------------------------- :: :: Welcome! This script is designed to automate the process of flushing :: user profiles within Windows 7, while at the same time preserving :: profiles of your choosing, including domain users. :: :: This script is written as an example of wanting all domain users wiped :: except for the one called "pctest". :: :: Portions of the script that will require manual edits will be preceded :: by instructions with these "double colon" comment marks. :: :: Please let me know how well (or not well) this works for you or any :: features you can think of that could be added. :: :: -------------------------------------- title Windows 7 User Profile Cleaning :: ---------- :: Add any users you wish to exclude from the wipe to the "userpreserve" :: line below and separate them by commas. Be careful - these are :: case-sensitive. :: ---------- :USERPRESERVE set userpreserve="Administrator,All Users,UpdatusUser,Default,Default User,Public,pctest" FOR /f "tokens=*" %%a IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECK "%%a" GOTO VERIFY :REGCHECK set SPACECHECK= FOR /f "tokens=3,4" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%b %%c FOR /f "tokens=2" %%d in ('echo %USERREGPATH%') DO SET SPACECHECK=%%d IF ["%SPACECHECK%"]==[""] GOTO REGCHECK2 GOTO USERCHECK :REGCHECK2 FOR /f "tokens=3" %%g in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%g GOTO USERCHECK :USERCHECK FOR /f "tokens=3 delims=\" %%e in ('echo %USERREGPATH%') DO SET USERREG=%%e FOR /f "tokens=1 delims=." %%f IN ('echo %USERREG%') DO SET USERREGPARSE=%%f ECHO %USERPRESERVE%|find /I "%USERREGPARSE%" > NUL IF ERRORLEVEL=1 GOTO CLEAN IF ERRORLEVEL=0 GOTO SKIP :SKIP ECHO Skipping user clean for %USERREG% GOTO :EOF :CLEAN ECHO Cleaning user profile for %USERREG% rmdir "C:\Users\%USERREG%" /s /q > NUL ECHO Cleaning user registry for %USERREG% reg delete %1 /f IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN1 GOTO :EOF :RETRYCLEAN1 ECHO Retrying clean of user profile %USERREG% rmdir "C:\Users\%USERREG%" /s /q > NUL IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN2 GOTO :EOF :RETRYCLEAN2 ECHO Retrying clean of user profile %USERREG% rmdir "C:\Users\%USERREG%" /s /q > NUL GOTO :EOF :VERIFY FOR /f "tokens=*" %%g IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECKV "%%g" GOTO REPORT :REGCHECKV set SPACECHECKV= FOR /f "tokens=3,4" %%h in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%h %%i FOR /f "tokens=2" %%j in ('echo %USERREGPATHV%') DO SET SPACECHECKV=%%j IF ["%SPACECHECKV%"]==[""] GOTO REGCHECKV2 GOTO USERCHECKV :REGCHECKV2 FOR /f "tokens=3" %%k in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%k GOTO USERCHECKV :USERCHECKV FOR /f "tokens=3 delims=\" %%l in ('echo %USERREGPATHV%') DO SET USERREGV=%%l FOR /f "tokens=1 delims=." %%m IN ('echo %USERREGV%') DO SET USERREGPARSEV=%%m ECHO %USERPRESERVE%|find /I "%USERREGPARSEV%" > NUL IF ERRORLEVEL=1 GOTO VERIFYERROR IF ERRORLEVEL=0 GOTO :EOF :VERIFYERROR SET USERERROR=YES GOTO :EOF :REPORT IF [%USERERROR%]==[YES] ( set RESULT=FAILURE ) ELSE ( set RESULT=SUCCESS ) :EXIT exit :EOF 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now