Good Morning,
I could use help, I'm trying to edit just some of the values of a REG_MULTI_SZ using PowerShell but I'm not having any luck.
The Key is: HKLM\SOFTWARE\Bentley\BentleyDesktopClient\Install
And the original value of the key is:
KeyName_CheckUpdateOption=
KeyName_CheckUpdateOptionModTime=<number-varies>
KeyName_UpdateClientInstallTime=<number-varies>
KeyName_CheckUpdateIsEnabled=1
(depending on the install, the values KeyName_CheckUpdateOptionModTime=<some-number>
and KeyName_UpdateClientInstallTime=<some-number>
will be different.)
I want the end result after editing to be:
KeyName_CheckUpdateOption=3
KeyName_CheckUpdateOptionModTime=<number-varies>
KeyName_UpdateClientInstallTime=<number-varies>
KeyName_CheckUpdateIsEnabled=0
But the best I've been able to come up with is replacing all the data with just one line with this script:
REG ADD HKLM\SOFTWARE\Bentley\BentleyDesktopClient\Install /t REG_MULTI_SZ /v "UpdateClient" /d "Keyname_CheckUpdateOption=3"
So the result I get is:
So the question is, how can I replace just SOME of the values in a Multi-String key, and preserve the rest and their order?
I would like to then create a Batch file so I can run this on multiple pc's.
I really appreciate everyone's time and assistance, Thank You.
CodePudding user response:
You could use switch
for this like below:
$regPath = 'HKLM:\SOFTWARE\Bentley\BentleyDesktopClient\Install'
$oldValue = (Get-ItemPropertyValue -Path $regPath -Name 'UpdateClient') -ne ''
$newValue = switch -Wildcard ($oldValue) {
'KeyName_CheckUpdateOption=*' { 'KeyName_CheckUpdateOption=3' }
'KeyName_CheckUpdateIsEnabled=*' { 'KeyName_CheckUpdateIsEnabled=0' }
default { $_ } # return this item unchanged
}
Set-ItemProperty -Path $regPath -Name 'UpdateClient' -Value $newValue
The new content of the registry value will be:
KeyName_CheckUpdateOption=3
KeyName_CheckUpdateOptionModTime=<number-varies>
KeyName_UpdateClientInstallTime=<number-varies>
KeyName_CheckUpdateIsEnabled=0
CodePudding user response:
This may be too much code lines of powershell, but it works for me:
Clear-Host
$currentProperty = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Bentley\BentleyDesktopClient\Install | Select-Object -ExpandProperty "UpdateClient")
#You may need this to compare in the future
$newProperty = $currentProperty
#Searching value KeyName_CheckUpdateOption. If value set - replace it, if not - set it
$CurrentKeyName_CheckUpdateOption = $null
$CurrentKeyName_CheckUpdateOption = ($newProperty | Select-String -Pattern 'KeyName_CheckUpdateOption=([0-9.] )')
if($CurrentKeyName_CheckUpdateOption.Matches.Value) {
Write-Output $CurrentKeyName_CheckUpdateOption.Matches.Value
$newProperty = $newProperty.Replace("$($CurrentKeyName_CheckUpdateOption.Matches.Value)","KeyName_CheckUpdateOption=3")
} else {
$newProperty = $newProperty.Replace("KeyName_CheckUpdateOption=","KeyName_CheckUpdateOption=3")
}
#Same for KeyName_CheckUpdateIsEnabled.
$CurrentKeyName_CheckUpdateIsEnabled = $null
$CurrentKeyName_CheckUpdateIsEnabled = ($newProperty | Select-String -Pattern 'KeyName_CheckUpdateIsEnabled=([0-9.] )')
if($CurrentKeyName_CheckUpdateIsEnabled.Matches.Value) {
$newProperty = $newProperty.Replace("$($CurrentKeyName_CheckUpdateIsEnabled.Matches.Value)","KeyName_CheckUpdateIsEnabled=0")
} else {
$newProperty = $newProperty.Replace("KeyName_CheckUpdateIsEnabled=","KeyName_CheckUpdateIsEnabled=0")
}
# Set updated value to registry
Set-ItemProperty -Path HKLM:\SOFTWARE\Bentley\BentleyDesktopClient\Install -Name UpdateClient -Value $newProperty