Search code examples
batch-fileregistryregedit

How to change regedit registry from command line?


I'm using "D:\users" profilePath for my Domain users. When I change my "systemdrive" with new windows image, Domain users creates new profile like "D:\Users\old.DNS".

I want to use old profilepath but I couldn't find any method to tell windows "use existing profile". Because of that I need to change profile path from regedit!

I tried to write something but I don't know how can I modify the registry only changes "dnsname".

Because username is different for every machine and also DNSname can be different for different companies.

I'm taking user sid with

set sid=wmic useraccount where name='%username%' get sid

%sid%

Then I need to take username and remove .DNS from it, but I don't know how can I do that. Can i write someting like "delete after "." "point" in batch?


Solution

  • I found my self and this script needs admin privileges.

    ::get usersid first
    for /F "tokens=2" %%i in ('whoami /user /fo table /nh') do set usersid=%%i
    
    ::get userpath
    for /F "tokens=3" %%i in ('reg QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%usersid%" /v "ProfileImagePath"') do set oldpath=%%i
    
    ::delete after "."
    for /f "tokens=1 delims=." %%a in ('echo %oldpath%') do set newpath=%%a
    
    ::update the reg with new path
    reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\%usersid%" /v "ProfileImagePath" /t REG_EXPAND_SZ /d "%newpath%" /f
    
    echo EVERYTHING OKAY MATE!
    pause