Reading registry values and then adding to it

I am trying to get a registry key value , ExtensionSettings, if there is something in the registry then save it. Then add a new entry to it . The two entries have to have a comma in-between values. I can not figure the code out . I am not sure what I am doing wrong or missing. Thank you so much for your help.

Set variables to indicate value and key to set

$RegistryPath = ‘HKLM:SOFTWARE\Policies\Google\Chrome’
$Name = ‘ExtensionSettings’
$Value = ‘{“feolagkacappiaieohahjkeaikhjjcfa”:{“toolbar_pin”:“force_pinned”}}’

Create the key if it does not exist

If (-NOT (Test-Path $RegistryPath)) {
New-Item -Path $RegistryPath -Force | Out-Null
}

$OldValue = (Get-ItemProperty “HKLM:SOFTWARE\Policies\Google\Chrome”).ExtensionSettings
$NewValue = New-ItemProperty -Path $RegistryPath -Name $Name -Value $value
$UpdatedValue = $OldValue + “,” + $NewValue

$NewValue = $NewValue.ExtensionSettings

New-ItemProperty -Path $RegistryPath -Name $Name -Value $UpdatedValue -PropertyType String -Force

Here is the error I get. I have updated the code with what I have tried . Nothing is written to the registry key. It goes show the comma but not the rest.

error I receive is

New-ItemProperty : The property already exists.

At line:15 char:13

  • $NewValue = New-ItemProperty -Path $RegistryPath -Name $Name -Value $ …
  •         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ResourceExists: (HKEY_LOCAL_MACH…s\Google\Chrome:String) [New-ItemProperty], IOException
    • FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand

mmoore5553,
Welcome to the forum. :wave:t4:

The error message is pretty obvious. The registry value already exists. You cannot create it new anymore. If you want to change an existing registry value you simply use

You can even use Set-ItemProperty to create new items. You just have to specify the item type then. :wink:

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

thank you for the welcome. I will do that next time. I did try the Set-ItemProperty and ran into issues. I will read over the article again. I am sure I am missing something simple.

The sad part is if I run Set-ItemProperty -Path $RegistryPath -Name $Name -Value $value just by itself it runs the command with no issues. It is only when I am combining multi entries new value does not show up.

Don’t descibe it - show it! :wink: If you have an issue with a particular piece of code you should share the complete code or at least the relevant part of it along with all error messages completely or if there are no error messages share it along with an explanation what’s not working as expected.

if I change to set-itemproperty then the commands complete but $newvalue has no value. It puts a blank in the registry.

so if test was the value the new results in the registry will be ,test There is no new value .

# Set variables to indicate value and key to set
$RegistryPath = 'HKLM:SOFTWARE\Policies\Google\Chrome'
$Name         = 'ExtensionSettings'
$Value        = '{"feolagkacappiaieohahjkeaikhjjcfa":{"toolbar_pin":"force_pinned"}}'

# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
  New-Item -Path $RegistryPath -Force | Out-Null
}  

$OldValue = (Get-ItemProperty "HKLM:SOFTWARE\Policies\Google\Chrome").ExtensionSettings
$NewValue =  Set-ItemProperty -Path $RegistryPath -Name $Name -Value $value
Write-Host $NewValue 
$UpdatedValue =  $NewValue + "," + $OldValue 
$NewValue = $NewValue.ExtensionSettings

Set-ItemProperty -Path $RegistryPath -Name $Name -Value $UpdatedValue  -Force  

If i go to the powershell ise window and just run

Set-ItemProperty -Path $RegistryPath -Name $Name -Value $value
the variables are written to the registry key with no problem. Just the one value. So i know $NewValue can work. I just do not know why it is not working like the way i have the script. I can change the value If I run individual code

Just to mention it at least once … you could replace "HKLM:SOFTWARE\Policies\Google\Chrome" with $RegistryPath. :wink:
If you just created the key there is no ItemProperty you could read.

With this line of code you overwrite the existing value if it already exists. But you do not assign anything to the variable $NewValue because the cmdlet does not return any output by default.

If you want to have the changed object returned you have to specify the parameter -PassThru.

Here is the final code. Thank you for the help olaf. It took me a minute to figure out what you was saying. This is for someone else that might have the same issue. It works now. WOOHOO

# Set variables to indicate value and key to set
$RegistryPath = 'HKLM:SOFTWARE\Policies\Google\Chrome'
$Name         = 'ExtensionSettings'
$Value        = '{"feolagkacappiaieohahjkeaikhjjcfa":{"toolbar_pin":"force_pinned"}}'

# Create the key if it does not exist
If (-NOT (Test-Path $RegistryPath)) {
  New-Item -Path $RegistryPath -Force | Out-Null
}  

$OldValue = (Get-ItemProperty $RegistryPath).ExtensionSettings
$NewValue =  Set-ItemProperty -Path $RegistryPath -Name $Name -Value $value -PassThru
#Write-Host $NewValue 
$UpdatedValue =  $NewValue.ExtensionSettings + "," + $OldValue 


Set-ItemProperty -Path $RegistryPath -Name $Name -Value $UpdatedValue  -Force 

Hey, great that it helped and thanks for sharing. :+1:t4: