How-To edit part of a registry value

0

I`m looking for å way to remove last the 3 letters from a registry value.

Example:

[HKEY_CURRENT_USER\Software\Test]

"Setting"="ABCDDD"

I would like it to end up like this:

[HKEY_CURRENT_USER\Software\Test]

"Setting"="ABC"

It is always 6 letters, and I want always to remove the last 3. I prefer to achieve this using GPO.

Thanks!

Regards Fredrik

Fredrik Endresen

Posted 2018-10-31T18:15:59.197

Reputation: 11

This site isn't a script writing service, what have you tried so far? – spikey_richie – 2018-10-31T19:34:07.613

Answers

0

As it's pretty short, this might start you off:

$registryPath = "HKCU:\Software\Test\" #reg key
$Name         = "setting"              #reg value
$value        = "ABCDDD"               #default value if does not exist

IF(!(Test-Path $registryPath))
{
    #Key does not exist, create key and set default
    New-Item -Path $registryPath -Force | Out-Null
    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType String -Force | Out-Null
}
ELSE 
{
    #Key exists, read existing value and trim
    $strValue = (Get-ItemProperty -Path $registryPath).($Name)
    $strNew   = $strValue.SubString(0,3)
    New-ItemProperty -Path $registryPath -Name $name -Value $strNew -PropertyType String -Force | Out-Null
}

HelpingHand

Posted 2018-10-31T18:15:59.197

Reputation: 1 435

Thanks a lot "HelpingHand"! This will do the trick! :) Can I ask you one more. Let use say the Value was: XXXZZZYYY and I want to trim\remove the 3 letters in the middle, and end up with a value like this: XXXYYY Would that also be possible with PS? Thanks again! – Fredrik Endresen – 2018-10-31T22:07:04.013

You could use the Remove method. I.e. change the SubString line to be: $strNew = $strValue.Remove(3,3) This will remove from strValue, the 3rd character the next 3 characters. You can change $value at the top to be XXXYYYZZZ and then you will just have XXXZZZ – HelpingHand – 2018-10-31T23:48:43.350

Thanks again - it works like a charm! One last question. :) In a Value field, I always want to change 2012 with 2016. Like: \servername\2012server\path\ -> \servername\2016server\path. Or it could be: TheYear2012WasGreat -> TheYear2016WasGreat . I just want to pick up 2012 from value string and change it to 2016. – Fredrik Endresen – 2018-11-01T22:25:47.983

Would it be sufficient to just use: $strNew = $strValue.replace("2012","2016") for that case? – HelpingHand – 2018-11-01T23:48:40.347