[SOLVED] Remove a user profile such as defaultuser0

Discussion in 'Scripting' started by mxman2k, Aug 22, 2016.

  1. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,679
    19,123
    180
    Some commands do seem to work better with a little timeout delay after they been used.

    WMIC calls can screw up if no delay is used, not always but it not hurt to allow a little time for them to complete.

    TAKEOWN & ICACLS should'nt usually as they 'pause' the script while they in action, but i now always give a little delay 1 sec is more than enough but the timeout /T 1 is the lowest 1 second amount it allows.

    You could use the old 'ping -n 1 -w 300 127.0.0.1 >NUL 2>&1' way but timeout looks better. That ping example is 300th of a second give or take.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. NM156

    NM156 MDL Novice

    Jun 10, 2016
    18
    7
    0
    Mind showing me an example of the structure of the oobe file i should have or how i would format it to use in in setup complete? I'm only guessing at it and have no idea if i'm formatting it correctly. I posted above what i did to my oobe.cmd file, does that look right?

    I've tried it multiple ways now none of them worked so i must be doing something wrong.
     
  3. mxman2k

    mxman2k MDL Developer

    Jun 20, 2007
    5,679
    19,123
    180
    #23 mxman2k, Apr 30, 2017
    Last edited: Apr 30, 2017
    (OP)
    Well i not use oobe it self, the defaultuser0 routine is now part of the add-ons within the MRP. The above code is the same as what is in the addon i use, although the only thing missing is the oobestate and REM's as MRP is running in OOBE ie SYSTEM mode so full access rights are there, beyond even a Administrator.

    Code:
    @echo off
    REM Rename Registered Owner To Users Name. [ W10/Server 2016 ] Subroutine Section.
    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
    
    IF EXIST "%SystemDrive%\Users\defaultuser0\" GOTO :RemDU0
    
    GOTO :Finish
    
    :RemDU0
    
    SET UserToRemove="defaultuser0"
    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
    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 :Dun
    RMDIR "%SystemDrive%\Users\%ProfileName%" /s /q >NUL 2>&1
    REG delete %1 /f >NUL 2>&1
    TIMEOUT /T 1 /NOBREAK >nul
    
    IF EXIST "%SystemDrive%\Users\defaultuser0\" GOTO :RetryDirRemove
    GOTO :Dun
    
    :RetryDirRemove
    TAKEOWN /F "%SystemDrive%\Users\%ProfileName%" >NUL 2>&1
    ICACLS "%SystemDrive%\Users\%ProfileName%" /GRANT *S-1-1-0:F >NUL 2>&1
    RMDIR "%SystemDrive%\Users\%ProfileName%" /s /q >NUL 2>&1
    TIMEOUT /T 1 /NOBREAK >nul
    
    GOTO :Dun
    
    :Finish
    REM Proper End of script... This self-deletes this batch file.
    DEL /F /Q %0% >nul
    
    :Dun
    REM Nothing End Of Script. DO NOT DELETE OR EDIT THIS LINE! - Used for subroutine.

    Save the above as UserZeroRem.cmd and place in the scripts folder alongside the oobe.cmd.

    In your oobe.cmd , if you add:

    Code:
    IF EXIST "%windir%\Setup\scripts\UserZeroRem.cmd" CALL "%windir%\Setup\Scripts\UserZeroRem.cmd"
    That should do it, other than that i am unsure what is going wrong.

    You can change the UserZeroRem.cmd to whatever name you wish as long as the filename in the CALL .... is the same.

    Use .cmd and not .bat as that can cause some odd effects in oobe mode. If the above file is used on a OS other than Windows 10 RS1+2 / Server 2016 you might screw up the OS's user profile list.

    In the Multi-Oem/Retail Project (Continued), there are multiple checks for OS version etc that will safely remove the defaultuser0 and not cause any problems to other OS's. It brands and legally activates via SLIC (win7/Vista) or MSDM (W8.x/10) if that is available.

    You can still edit the Oobe with your tweaks etc but note that the username or the registry (HKCU) User Hive in the registry is NOT created at that point, oobe.cmd runs BEFORE the user is setup in the Windows Setup program. SetupComplete i think runs just after OOBE.cmd ie after the user name/registry (HKCU) User Hive is created.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. dhjohns

    dhjohns MDL Guru

    Sep 5, 2013
    3,262
    1,731
    120
    This should read -- Note: Must be run as Administrator!

    No real criticism, but the conjugation is incorrect.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. NM156

    NM156 MDL Novice

    Jun 10, 2016
    18
    7
    0
    Well i was doing it both way, i guess it's just not going to work for me. Spent all night last night and a few hours today and made like a dozen isos ( with some other stuff i was working out as well, not just that ) But that's the last thing that just won't work for me. Well, it's easy enough to remove after the fact so i might just stick with that.

    By the way the script still doesn't actually remove defaultuser0 completely if you run it with admin rights after install, defaultuser0 is still there just disabled. It shows up in a net users command and also in lusrmgr.msc. You can delete it there and then clean up the registry as well, as there is still a lot of references to it. Which is basically how i was doing it before manually anyway. At least on my end that's what is happening.

    Anyway, thanks for your time and help, i've at least learned some stuff just messing around in that setup folder i knew nothing about before, so that's good.
     
  6. qkwxx

    qkwxx MDL Novice

    Jul 22, 2015
    17
    2
    0
    #26 qkwxx, Jun 28, 2017
    Last edited: Jun 28, 2017
    Open Local Users and Groups, and click/tap on the Users folder in the left pane to open it.

    defaultuser0 ?

    NET.exe user defaultuser0 /delete
     
  7. hearywarlot

    hearywarlot MDL Member

    Jul 31, 2015
    112
    153
    10
    #28 hearywarlot, Jul 25, 2017
    Last edited: Jul 25, 2017
    You might wanna look at VBS 'shell.application' object to automatically run your script as privileged user.
    You can test some command that succeeds on correct privileges, such as 'FSUTIL dirty query' and otherwise fails.

    Take a look at my script for elevation code that can run from Batch file without extracting and additionally supports passing arguments with special characters.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...