Search code examples
windowsbatch-fileregistrywindows-10

Changes to registry via batch/.reg file not persisting after restart Windows 10


I'm making a script to change the target paths for a few user folders.

Here is my .reg file:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
"Personal"="C:\\Users\\%username%\\Google Drive\\Documents\\"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
"Desktop"="C:\\Users\\%username%\\Google Drive\\Desktop\\"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
"Downloads"="C:\\Users\\%username%\\Google Drive\\Downloads\\"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
"My Pictures"="C:\\Users\\%username%\\Google Drive\\Pictures\\"

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
"Favorites"="C:\\Users\\%username%\\Google Drive\\Favorites\\"

It changes the registry setting perfectly. However, I restart the box and it's still going to default %userprofile%\documents. If I click the property tab on the folder and when I check the registry it is still changed from my script. If I do it manually it works. However, I'm scripting it to run it on 50 machines. I have done this before in XP without any issue. I have no idea what I'm doing wrong.

I also tried changing it in shell folders as well to no avail. I ran an app called whatchanged to capture the reg setting that are being modified when I change the target manually, but it captured 70 changes! I've tried running it as administrator, creating a folder in root C:\ with permissions for everyone but to no avail.

I also tried the syntax like this:

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders] "Favorites"="C:\\Users\\%username%\\Google Drive\\Favorites\\"

Solution

  • Used syntax implies REG_SZ data type:

    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
    "Personal"="%USERPROFILE%\\Documents"
    

    You need REG_EXPAND_SZ data type; it should be as follows for above example:

    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders]
    "Personal"=hex(2):25,00,55,00,53,00,45,00,52,00,50,00,52,00,4f,00,46,00,49,00,\
      4c,00,45,00,25,00,5c,00,44,00,6f,00,63,00,75,00,6d,00,65,00,6e,00,74,00,73,\
      00,00,00
    

    I'd use REG.exe in a batch-file script, something like

    @echo off
    SETLOCAL EnableExtensions
    set "_KeyName=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
    reg add "%_KeyName%"  /v Personal /t REG_EXPAND_SZ /d "%%USERPROFILE%%\Documents" /f