Remove defaultuser0 Profile

Discussion in 'Windows 10' started by l33tissw00t, Nov 29, 2016.

Thread Status:
Not open for further replies.
  1. l33tissw00t

    l33tissw00t MDL Addicted

    Dec 6, 2012
    542
    314
    30
    #1 l33tissw00t, Nov 29, 2016
    Last edited by a moderator: Apr 20, 2017
    Don't remember who posted this, but it's quite handy:
    Code:
    REM ** Must be ran as Administrator to function correctly. **
    REM Simple script to check for defaultuser0 , correct Windows User name to account name, delete defaultuser0 files and clean up registry area.
    REM -
    REM  Designed for Windows 10 RS1/Anniversary Update.
    
    SET OobeState=0
    REG QUERY "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup\State" | findstr /I "\<IMAGE_STATE_COMPLETE\>" >nul && IF ERRORLEVEL=1 SET OobeState=1
    REM Returns - Oobestate 0 = not running via oobe.  1 = setup not complete ie within Windows setup's oobe stage.
    
    REM         add REM before, or delete the line below to allow RegisteredOwner rename via manual method if required.
    IF %OobeState%==0 GOTO :StartMain
    
    REM To change the RegisteredOwner registry entry to current username. 
    REM It has to be deleted then re-added or fails tried /ve and other ways , only delete and remake works properly??
        IF EXIST "%SystemDrive%\Users\defaultuser0\" (
        REG DELETE "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v "RegisteredOwner" /f >nul 2>&1
        REG ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion" /v "RegisteredOwner" /t REG_SZ /d "%UserName%" /f >nul 2>&1
        )
    
    :StartMain
    SET UserToRemove="defaultuser0"
    REM 'CALL' check registry subroutine check for existance and remove.
    FOR /f "tokens=*" %%a IN ('REG QUERY "HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList"^|find /i "s-1-5-21"') DO CALL :ChkRegistry "%%a"
    GOTO :Finish
    
    :ChkRegistry
    REM -----------------------------------------------------------------------
    REM Results of ERRORLEVEL are:
    REM 0  -- If UserToRemove is found in the registry then run rest of script.
    REM 1  -- If UserToRemove is NOT found in the registry then exit script.
    REM -----------------------------------------------------------------------
    FOR /f "tokens=3" %%g in ('reg query %1 /v ProfileImagePath') DO SET UserProfilePath=%%g
    FOR /f "tokens=3 delims=\" %%e in ('echo %UserProfilePath%') DO SET ProfileName=%%e
    FOR /f "tokens=1 delims=." %%f IN ('echo %ProfileName%') DO SET ParseRegistry=%%f
    ECHO %UserToRemove%|find /I "%ParseRegistry%" > NUL
    IF ERRORLEVEL=1 GOTO :Finish
    
    REM Remove that user...
    RMDIR "%SystemDrive%\Users\%ProfileName%" /s /q
    REG delete %1 /f >nul
    IF EXIST "%SystemDrive%\Users\%ProfileName%" GOTO :RetryDirRemove
    GOTO :Finish
    
    :RetryDirRemove
    TAKEOWN /F "%ProfileName%" >nul
    TIMEOUT /T 2 >nul
    ICACLS "%ProfileName%" /GRANT *S-1-1-0:F >nul
    TIMEOUT /T 2 >nul
    RMDIR "%SystemDrive%\Users\%ProfileName%" /s /q
    
    :Finish
    REM End of script...

    Edit:
    Nevermind.. it's here :) Maybe it should be a link/sticky in Windows 10 forum.
     
  2. Enthousiast

    Enthousiast MDL Tester

    Oct 30, 2009
    13,708
    15,236
    340
    #2 Enthousiast, Nov 29, 2016
    Last edited by a moderator: Apr 20, 2017
    The manual method is in yellow at the OP of the sticky 14393 thread.
     
  3. l33tissw00t

    l33tissw00t MDL Addicted

    Dec 6, 2012
    542
    314
    30
    Oh, cool, thanks!
    Never knew that this (defaultuser0) was related to that (Windows User).