Powershell and VB script to update a registry key if it exists on the local system
Here are a couple scripts I wrote to update a registry key on a system if it the key exists. I created a PowerShell script to do this then realized that some of the systems being updated were pretty old so decided to go with VB Script Power Shell Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$keypath = "HKLM:\SOFTWARE\SomeSoftware\Key" $keypath32 = "HKLM:\SOFTWARE\Wow6432Node\SomeSoftware\Key" $regkey = "key" $keyvalue = "keyValue" Push-Location if(Test-Path $keypath) { Set-Location $keypath Set-ItemProperty .$regkey $keyvalue if(Test-Path $keypath32) { Set-Location $keypath32 Set-ItemProperty .$regkey $keyvalue } } elseif(Test-Path $keypath32) { Set-Location $keypath32 Set-ItemProperty .$regkey $keyvalue } Pop-Location exit 0 |
VB Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
Const HKCR = &H80000000 'HKEY_CLASSES_ROOT Const HKCU = &H80000001 'HKEY_CURRENT_USER Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE Const HKUS = &H80000003 'HKEY_USERS Const HKCC = &H80000005 'HKEY_CURRENT_CONFIG RegkeyPath = "SOFTWARE\SomeSoftware\Key" RegkeyPath32 = "SOFTWARE\Wow6432Node\SomeSoftware\Key" RegKey = "Key" keyvalue = "KeyValue" Function KeyExists(Key, KeyPath) Dim oReg: Set oReg = GetObject("winmgmts:!root/default:StdRegProv") If oReg.EnumKey(Key, KeyPath, arrSubKeys) = 0 Then KeyExists = True Else KeyExists = False End If End Function Dim oReg: Set oReg = GetObject("winmgmts:!root/default:StdRegProv") if KeyExists(HKLM, RegkeyPath) = True Then if KeyExists(HKLM, RegkeyPath32) = True Then oReg.SetStringValue HKLM, RegKeyPath32, RegKey, keyvalue end if oReg.SetStringValue HKLM, RegKeyPath, RegKey, keyvalue elseif KeyExists(HKLM, RegKeyPath32) = True Then oReg.SetStringValue HKLM, RegKeyPath32, RegKey, keyvalue else end if |
[…]