The following script does a check if a key exists, if an entry exists and if it has a predefined value, otherwise it updates the registry key with the desired value. Adapt the variables: $regPath (path to the registry key), $valueName (provide the name of the key), $requiredvalue (provide the desired value). Also set $type to the desired value (Dword, String, …)
<#
.DESCRIPTION
This script will test existence of registry key, registry entry and
its value to match with required value and provide the result
Author: Hansjoerg Morandell
Website: inmyplace.info
Version: 1.0.0
#>
# Provide registry key path
$regPath = "HKCU:\Software" # full path here
# Provide registry entry display name
$valueName = "<VALUENAME>"
# Provide registry entry expected value
$requiredValue = "<REQUIREDVALUE>"
# Provide registry type
$type = "String"
$regkeyexists = Test-Path -Path $regPath
if ($regkeyexists) {
# Check if registry entry named PbxServer exists
$regentryexists = Get-ItemProperty -Path $regPath -Name $valueName -ErrorAction SilentlyContinue
if ($regentryexists) {
# If registry entry named PbxServer exists, then fetch its value
$currentValue = Get-ItemProperty -Path $regPath | Select-Object -ExpandProperty $valueName -ErrorAction SilentlyContinue
# Match $requiredValue registry entry value with required value
if ($currentValue -eq $requiredvalue) {
Write-Host "Reg value exists and matching the required value."
} else {
Write-Host "Reg value exists, but does not match the required value."
Write-Host "Current value: $currentValue"
Write-Host "Required value: $requiredValue"
Set-ItemProperty -Path $regPath -Name $valueName -Value $requiredValue -Type $type -Force
}
}
else {
New-ItemProperty -Path $regPath -Name $valueName -Value $requiredValue -Type $type -Force
Write-Host "Registry value does not exist."
}
}
else {
New-Item -Path $regPath -Force
New-ItemProperty -Path $regPath -Name $valueName -Value $requiredValue -Type $type -Force
Write-Host "Registry key does not exist. Creating it"
}
this can also be used in a custom detection script in Intune to check if a registry key exists and has a desired value:
<#
.DESCRIPTION
This script will test existence of registry key, registry entry and
its value to match with required value and provide the result
Author: Hansjoerg Morandell
Website: inmyplace.info
Version: 1.0.0
#>
# Provide registry key path
$regPath = "HKCU:\Software" # full path here
# Provide registry entry display name
$valueName = "<VALUENAME>"
# Provide registry entry expected value
$requiredValue = "<REQUIREDVALUE>"
$regkeyexists = Test-Path -Path $regPath
if ($regkeyexists) {
#Check if registry entry named $valueName exists
$regentryexists = Get-ItemProperty -Path $regpath -Name $valueName -ErrorAction SilentlyContinue
if ($regentryexists) {
#If registry entry named PbxServer exists, then fetch its value
$currentValue = Get-ItemProperty -Path $regpath | Select-Object -ExpandProperty $valueName -ErrorAction SilentlyContinue
#Match PbxServer registry entry value with requied value
if ($currentValue -eq $requiredvalue) {
#Write-Host "Reg value exists and matching the required value."
Write-Host "Success"
Exit 0
} else {
#Write-Host "Reg value exists, but does not match the required value."
#Write-Host "Current value: $currentValue"
#Write-Host "Required value: $requiredValue"
Exit 1
}
}
else {
#Write-Host "Registry value does not exist."
Exit 1
}
}
else {
#Write-Host "Registry key does not exist."
Exit 1
}