LeightonJames Posted May 13, 2015 Posted May 13, 2015 (edited) Hi all I am revisiting an old experiment I was working on. Where I work we don't delete the User Profile folders, instead we move them to a folder on the Local Machine. I have managed to find a vbs which iterates through the registry and removes entries matching a specified regex query. The script in question is this one: http://automatones.net/programs/vbs/WindowsHelpScripts/deleteProfile.vbs It works great however it deletes the user's folder but I cannot workout how to modify the script to match my requirements. Instead of deleting the folders we want to move them to C:\Users\OP If this folder doesn't exist it will be created before the script is run. Anyone able to help me identify how to make the changes? Thanks Edited May 13, 2015 by elsiegee40
kennysarmy Posted May 13, 2015 Posted May 13, 2015 could you amend this script we use? @echo off goto MAINMENU :: PLEASE NOTE: This code was created by another user online, and has been edited to allow for additional control over which profiles should be deleted :: and which ones should be preserved always. The main body of the script is a quite complex system, and as such, I cannot adequately comment on each line. :: Therefore any changes that have been made by myself, I will explain. :MAINMENU :: Echoes out the menu numbers for the user to navigate from. cls echo. echo ***Windows 7 User Profile Cleaner****************** echo * * echo * This script automates the process of deleting * echo * user profiles on Windows 7 - please note that * echo * certain users are protected from this process * echo * such as Balcarras, Administrator, Sadmin etc. * echo * * echo * 1 Delete all user profiles * echo * 2 Delete all except for the specified user * echo * 3 Display C:\Users folder * echo * * echo * 4 Exit * echo * * echo *************************************************** echo. :CHOICE :: Sets the user input to run a specific section of code e.g: ":mainmenu". set /p C=[1,2,3,4]? if "%C%"=="4" goto EXIT if "%C%"=="3" goto USERS if "%C%"=="2" goto DELETEEXCLUDE if "%C%"=="1" goto DELETEALL goto MAINMENU :DELETEALL title Windows 7 User Profile Cleaning :USERPRESERVE :: Adds a preserve function, that stops the code from running if the user profiles match one of these names below. :: The %exclude% variable allows for the technician to add their own exception when running the script. set userpreserve="Administrator,All Users,UpdatusUser,Default,Default User,Public,sadmin,balcarras,Admin,mar,%exclude%" 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 pause GOTO mainmenu :DELETEEXCLUDE :: Allows the technician to prevent a user profile from being deleted when the rest of the users are. :: Stores their answer into the %exclude% variable which is referenced within the code at the start. SET /P exclude="Enter the username of the profile to be kept:" cls :: Shows the technician which name they chose to exclude from the process. ECHO The username you entered was: %exclude% ECHO Proceed with profile clean? pause GOTO deleteall :USERS :: Views the C:\Users directory so that the technician can see if the script was successful. %SystemRoot%\explorer.exe "C:\Users" pause GOTO MAINMENU :EXIT end
LeightonJames Posted May 13, 2015 Author Posted May 13, 2015 Possibly, should have mentioned though that we only want to delete the network profiles off the machine hence the reason for regex query. S-1-5-21-1537096949 This is the first part of the SID that we need to match, all our Network profiles start with this SID, local profiles are excluded from the delete routine automatically
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