Change Default Registry Value Using PowerShell

Thomas M 26 Reputation points
2022-11-28T23:10:18.607+00:00

Hello! I am trying to change a default registry value using PowerShell. I found an article online dated March 14, 2022 which indicates that default values can only be changed using reg add. From that article I was able to get the following command to work:

 reg add HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\LSD\4\Server\NfoPath\EasyLaw10 /d "\\ServerName\FolderName" /f  

However, the following command does not work:

 reg add HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\LSD\4\Server\NfoPath\EasyLaw10 /d "C:\Program Files (x86)\Folder Name" /f  

This is an issue because when all is said and done, the registry value needs to be:

 \\ServerName\FolderName; C:\Program Files (x86)\Folder Name  

I am sure that the problem is improper handling on the spaces in the second folder path. I have tried various combinations of single quotes and double quotes, but can't see to get the right syntax. I'm not sure this is really even a PowerShell issue, but I wasn't certain where else to pose the question, and I figured people here would know. I could just use a little help on this.

Thanks in advance for any help that you can provide!

--Tom

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,455 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 32,746 Reputation points
    2022-11-29T00:06:16.517+00:00

    This is an issue because when all is said and done, the registry value needs to be:

    Try this format in Powershell. (I used a test key.)

    reg add "HKEY_CURRENT_USER\Software\Test" /d '\\ServerName\FolderName; C:\Program Files (x86)\Folder Name' /f  
    

    That produces...

    PS C:\> reg add "HKEY_CURRENT_USER\Software\Test" /d '\\ServerName\FolderName; C:\Program Files (x86)\Folder Name' /f  
    The operation completed successfully.  
    PS C:\> reg query "HKEY_CURRENT_USER\Software\Test"  
      
    HKEY_CURRENT_USER\Software\Test  
        (Default)    REG_SZ    \\ServerName\FolderName; C:\Program Files (x86)\Folder Name  
    

    If you want quotes around the folder names, use this.

    reg add "HKEY_CURRENT_USER\Software\Test" /d '\"\\ServerName\FolderName\"; \"C:\Program Files (x86)\Folder Name\"' /f  
    

    Output...

    PS C:\> reg add "HKEY_CURRENT_USER\Software\Test" /d '\"\\ServerName\FolderName\"; \"C:\Program Files (x86)\Folder Name\"' /f  
    The operation completed successfully.  
    PS C:\> reg query "HKEY_CURRENT_USER\Software\Test"  
      
    HKEY_CURRENT_USER\Software\Test  
        (Default)    REG_SZ    "\\ServerName\FolderName"; "C:\Program Files (x86)\Folder Name"  
    

    Other ways to quote within quote.

    PS C:\> "`"xxxx zzzz`""  
    "xxxx zzzz"  
    PS C:\> """xxxx zzzz"""  
    "xxxx zzzz"  
    
    0 comments No comments