+ Post New Thread
Results 1 to 5 of 5
Scripts Thread, automate User profile deletion - Windows 7 in Coding and Web Development; Ok....... I am trying to automate the removal of user profiles on Windows 7 and we have found that DelProf2 ...
  1. #1


    Join Date
    Sep 2009
    Posts
    562
    Thank Post
    13
    Thanked 54 Times in 33 Posts
    Rep Power
    223

    automate User profile deletion - Windows 7

    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?

  2. #2

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    10,991
    Thank Post
    851
    Thanked 2,653 Times in 2,253 Posts
    Blog Entries
    9
    Rep Power
    764
    Powershell is going to be the best tool for this, it can mess with the registry like it is part of the file system.

  3. #3


    Join Date
    Sep 2009
    Posts
    562
    Thank Post
    13
    Thanked 54 Times in 33 Posts
    Rep Power
    223
    I've never used Powershell but I had a feeling it might be the winner in this situation.

  4. #4

    SYNACK's Avatar
    Join Date
    Oct 2007
    Posts
    10,991
    Thank Post
    851
    Thanked 2,653 Times in 2,253 Posts
    Blog Entries
    9
    Rep Power
    764
    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.

  5. #5

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,773
    Thank Post
    865
    Thanked 1,663 Times in 1,448 Posts
    Blog Entries
    11
    Rep Power
    442
    That batch file will do it

    Code:
    @ECHO OFF
    
    :: --------------------------------------
    ::
    :: Windows 7 User Profile Cleaning Script
    :: Version 3.1
    ::
    :: Written by Mike Stone
    :: mikestone@gmail.com
    :: 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

  6. Thanks to FN-GM from:

    IWDave (25th January 2014)

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 22
    Last Post: 12th February 2014, 11:03 AM
  2. Replies: 18
    Last Post: 13th August 2012, 05:58 PM
  3. Reset User Profile in Window Active Directory
    By doombadger in forum Windows Server 2008
    Replies: 3
    Last Post: 14th May 2012, 08:20 PM
  4. Replies: 13
    Last Post: 13th September 2006, 07:42 AM
  5. User profile deletion, XP Pro
    By Sirbendy in forum Windows
    Replies: 7
    Last Post: 16th January 2006, 05:00 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •