User Profile Clean Version 3.0


Archive of User Profile Cleaning Script.

Version 3.0:

@ECHO OFF

:: --------------------------------------
::
:: Windows 7 User Profile Cleaning Script
:: Version 3.0
::
:: Written by Mike Stone
:: mikestone@gmail.com
:: https://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
FOR /f "tokens=3" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%b
FOR /f "tokens=3 delims=\" %%c in ('echo %USERREGPATH%') DO SET USERREG=%%c
FOR /f "tokens=1 delims=." %%d IN ('echo %USERREG%') DO SET USERREGPARSE=%%d

:USERCHECK
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=*" %%e IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECKV "%%e"
GOTO REPORT

:REGCHECKV
FOR /f "tokens=3" %%f in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%f
FOR /f "tokens=3 delims=\" %%g in ('echo %USERREGPATH%') DO SET USERREGV=%%g
FOR /f "tokens=1 delims=." %%h IN ('echo %USERREG%') DO SET USERREGPARSEV=%%h

:USERCHECKV
ping 127.0.0.1 -n 3 > NUL
ECHO %USERPRESERVE%|find /I "%USERREGPARSEV%"
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
)

:: ----------
:: This is fairly optional - it's just something I added so
:: that I could keep an eye on the labs remotely to make
:: sure there weren't masses of critical errors with the
:: script failing.
::
:: If you don't want it, just comment-out or remove the
:: "net use" line.
::
:: If you do want it, then make the necessary modifications
:: to the net use to map an appropriate sharepoint.
:: ----------

net use t: \\server\share

IF EXIST "t:\labreport.txt" (
	GOTO REPORTGEN
) ELSE (
	GOTO EXIT
)

:REPORTGEN
FOR /F "tokens=*" %%i in ('echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,2%-%time:~3,2%-%time:~6,2%') DO SET TDATETIME=%%i
FOR /F "tokens=14" %%j in ('ipconfig^|find "IPv4 Address"') DO SET IPNUMBER=%%j
ECHO. %RESULT%	%COMPUTERNAME%	%IPNUMBER%	%TDATETIME%>>"t:\labreport.txt"
net use t: /delete
GOTO EXIT

:EXIT
exit

:EOF
Advertisements