Guest User

Untitled

a guest
Oct 7th, 2016
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. @ECHO OFF
  2.  
  3. :: --------------------------------------
  4. ::
  5. :: Windows 7 User Profile Cleaning Script
  6. :: Version 3.1
  7. :: --------------------------------------
  8. ::
  9. :: Welcome! This script is designed to automate the process of flushing
  10. :: user profiles within Windows 7, while at the same time preserving
  11. :: profiles of your choosing, including domain users.
  12. ::
  13. :: This script is written as an example of wanting all domain users wiped
  14. :: except for the one called "dadmin".
  15. ::
  16. :: Portions of the script that will require manual edits will be preceded
  17. :: by instructions with these "double colon" comment marks.
  18. ::
  19. :: Please let me know how well (or not well) this works for you or any
  20. :: features you can think of that could be added.
  21. ::
  22. :: --------------------------------------
  23.  
  24. title Windows 7 User Profile Cleaning
  25.  
  26. :: ----------
  27. :: Add any users you wish to exclude from the wipe to the "userpreserve"
  28. :: line below and separate them by commas. Be careful - these are
  29. :: case-sensitive.
  30. :: ----------
  31.  
  32. :USERPRESERVE
  33. set userpreserve="Administrator,All Users,UpdatusUser,Default,Default User,Public,dadmin,DefaultAppPool"
  34.  
  35. FOR /f "tokens=*" %%a IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECK "%%a"
  36. GOTO VERIFY
  37.  
  38. :REGCHECK
  39. set SPACECHECK=
  40. FOR /f "tokens=3,4" %%b in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%b %%c
  41. FOR /f "tokens=2" %%d in ('echo %USERREGPATH%') DO SET SPACECHECK=%%d
  42. IF ["%SPACECHECK%"]==[""] GOTO REGCHECK2
  43. GOTO USERCHECK
  44.  
  45. :REGCHECK2
  46. FOR /f "tokens=3" %%g in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATH=%%g
  47. GOTO USERCHECK
  48.  
  49. :USERCHECK
  50. FOR /f "tokens=3 delims=\" %%e in ('echo %USERREGPATH%') DO SET USERREG=%%e
  51. FOR /f "tokens=1 delims=." %%f IN ('echo %USERREG%') DO SET USERREGPARSE=%%f
  52. ECHO %USERPRESERVE%|find /I "%USERREGPARSE%" > NUL
  53. IF ERRORLEVEL=1 GOTO CLEAN
  54. IF ERRORLEVEL=0 GOTO SKIP
  55.  
  56. :SKIP
  57. ECHO Skipping user clean for %USERREG%
  58. GOTO :EOF
  59.  
  60. :CLEAN
  61. ECHO Cleaning user profile for %USERREG%
  62. rmdir "C:\Users\%USERREG%" /s /q > NUL
  63. ECHO Cleaning user registry for %USERREG%
  64. reg delete %1 /f
  65. IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN1
  66. GOTO :EOF
  67.  
  68. :RETRYCLEAN1
  69. ECHO Retrying clean of user profile %USERREG%
  70. rmdir "C:\Users\%USERREG%" /s /q > NUL
  71. IF EXIST "C:\Users\%USERREG%" GOTO RETRYCLEAN2
  72. GOTO :EOF
  73.  
  74. :RETRYCLEAN2
  75. ECHO Retrying clean of user profile %USERREG%
  76. rmdir "C:\Users\%USERREG%" /s /q > NUL
  77. GOTO :EOF
  78.  
  79. :VERIFY
  80. FOR /f "tokens=*" %%g IN ('reg query "hklm\software\microsoft\windows nt\currentversion\profilelist"^|find /i "s-1-5-21"') DO CALL :REGCHECKV "%%g"
  81. GOTO REPORT
  82.  
  83. :REGCHECKV
  84. set SPACECHECKV=
  85. FOR /f "tokens=3,4" %%h in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%h %%i
  86. FOR /f "tokens=2" %%j in ('echo %USERREGPATHV%') DO SET SPACECHECKV=%%j
  87. IF ["%SPACECHECKV%"]==[""] GOTO REGCHECKV2
  88. GOTO USERCHECKV
  89.  
  90. :REGCHECKV2
  91. FOR /f "tokens=3" %%k in ('reg query %1 /v ProfileImagePath') DO SET USERREGPATHV=%%k
  92. GOTO USERCHECKV
  93.  
  94. :USERCHECKV
  95. FOR /f "tokens=3 delims=\" %%l in ('echo %USERREGPATHV%') DO SET USERREGV=%%l
  96. FOR /f "tokens=1 delims=." %%m IN ('echo %USERREGV%') DO SET USERREGPARSEV=%%m
  97. ECHO %USERPRESERVE%|find /I "%USERREGPARSEV%" > NUL
  98. IF ERRORLEVEL=1 GOTO VERIFYERROR
  99. IF ERRORLEVEL=0 GOTO :EOF
  100.  
  101. :VERIFYERROR
  102. SET USERERROR=YES
  103. GOTO :EOF
  104.  
  105. :REPORT
  106. IF [%USERERROR%]==[YES] (
  107. set RESULT=FAILURE
  108. ) ELSE (
  109. set RESULT=SUCCESS
  110. )
  111.  
  112. :: ----------
  113. :: This is fairly optional - it's just something I added so
  114. :: that I could keep an eye on the labs remotely to make
  115. :: sure there weren't masses of critical errors with the
  116. :: script failing.
  117. ::
  118. :: If you don't want it, just comment-out or remove the
  119. :: "net use" line.
  120. ::
  121. :: If you do want it, then make the necessary modifications
  122. :: to the net use to map an appropriate sharepoint.
  123. :: ----------
  124.  
  125. ::net use t: \\server\share
  126.  
  127. IF EXIST "C:\labreport.txt" (
  128. GOTO REPORTGEN
  129. ) ELSE (
  130. GOTO EXIT
  131. )
  132.  
  133. :REPORTGEN
  134. FOR /F "tokens=*" %%n in ('echo %date:~10,4%-%date:~4,2%-%date:~7,2% %time:~0,2%-%time:~3,2%-%time:~6,2%') DO SET TDATETIME=%%n
  135. FOR /F "tokens=14" %%o in ('ipconfig^|find "IPv4 Address"') DO SET IPNUMBER=%%o
  136. ECHO. %RESULT% %COMPUTERNAME% %IPNUMBER% %TDATETIME%>>"C:\labreport.txt"
  137. ::net use t: /delete
  138. GOTO EXIT
  139.  
  140. :EXIT
  141. exit
  142.  
  143. :EOF
Add Comment
Please, Sign In to add comment