Modifying Registry Keys - Add to if not already present
Hello PowerShell Users,
Pre-face: I am very new to PowerShell scripting and until this point have only really used a handful of select commands.
I am currently playing with PSADT for app deployments and as a post-installation task, I am trying to write a script to check an existing multistring registry key and if certain values are missing, add them.
I feel like I am missing something obvious or really over-thinking it but my google-fu is not powerful enough.
Currently, I am hitting this error:
parsing "HKEY_LOCAL_MACHINE\SOFTWARE\PISystem
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\PISystem" - Malformed \p{X} character escape.
At line:15 char:1
+ $entryExists = $CurrentValue -match $ExpectedEntries
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
Script:
The value of $CurrentValue in this instance is a multistring registry value of "HKEY_LOCAL_MACHINE\SOFTWARE\PISystem
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\PISystem
"
Below is the full script:
# Set path to registry key
$RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Office\ClickToRun\REGISTRY\MACHINE\Software\Microsoft\AppV\Subsystem\VirtualRegistry'
$Name = 'PassThroughPaths'
# Get current value for registry key
$CurrentValue = Get-ItemPropertyValue -Path $RegistryPath -Name $Name
# Setup the desired entry
$ExpectedEntries = @"
HKEY_LOCAL_MACHINE\SOFTWARE\PISystem
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\PISystem
"@
# Check if the desired entry already exists
$entryExists = $CurrentValue -match $ExpectedEntries
if (-not $entryExists) {
# Append the entry to the file
$testValue = $CurrentValue + $ExpectedEntries
} else {
# Entry already exists
$testValue = $CurrentValue
}
$testValue
# Now $content contains the updated data
# You can save it back to the registry or use it as needed
Set-ItemProperty -Path $RegistryPath -Name $Name -Value $testValue
any help would be very welcome