Public/Replace-SpecRegistryKey.ps1

# This is a helper function that uses new-specregistrykey with set-specregistryvalue to replace a registry key and all of its values.
Function Replace-SpecRegistryKey {
    <#
    .SYNOPSIS
    This function replaces or creates a registry key with the specified path, value name, data, and value type.
 
    .DESCRIPTION
    The Replace-SpecRegistryKey function checks if the specified registry key exists. If it does, the function updates the key with the provided data and returns 110. If the key does not exist, it creates a new registry key with the specified path, value name, data, and value type and returns 111. If there is an error it returns 1.
 
    .PARAMETER KeyPath
    Specifies the registry path where the key should be located or created.
 
    .PARAMETER ValueName
    Specifies the name of the registry value to be updated or created.
 
    .PARAMETER ValueData
    Specifies the data to be set for the registry value.
 
    .PARAMETER ValueType
    Specifies the data type of the registry value. Valid values are 'String' and 'DWord'.
 
    .EXAMPLE
    Replace-SpecRegistryKey -KeyPath "HKLM:\Software\Example" -ValueName "SampleValue" -ValueData "TestData" -ValueType "String"
    Updates or creates a registry key "SampleValue" with data "TestData" of type String under the path "HKLM:\Software\Example".
 
    .EXAMPLE
    Replace-SpecRegistryKey -KeyPath "HKLM:\Software\Example" -ValueName "SampleValue" -ValueData 123 -ValueType "DWord"
    Updates or creates a registry key "SampleValue" with data 123 of type DWord under the path "HKLM:\Software\Example".
 
    .NOTES
    Return Codes:
    110 - Registry key updated
    111 - Registry key created
    1 - Error
 
    Author : owen.heaume
    Version : 1.0
    #>

    [cmdletbinding()]

    param (
        [Parameter (mandatory = $true)]
        [string]$KeyPath,

        [Parameter (mandatory = $true)]
        [string]$ValueName,

        [Parameter (mandatory = $true)]
        [string]$ValueData,

        [Parameter (mandatory = $true)]
        [ValidateSet('String', 'DWord')]
        [string]$ValueType
    )

    # If any part of the reg path doesn't exist at all, this will create it (New-Spec...), if the value name already exists, it just updates (Set-Spec...)
    try {
        write-verbose "Attempting to update the registry key $KeyPath $ValueName $ValueData $ValueType"
        if (Get-ItemProperty -Path $KeyPath -Name $ValueName -ErrorAction SilentlyContinue) {
            $null = Set-SpecRegistryKey -KeyPath $KeyPath -ValueName $ValueName -ValueType $ValueType -ValueData $ValueData
            write-verbose "Successfully updated the registry key"
            return 110
        } else {
            $null = New-SpecRegistryKey -KeyPath $keypath -ValueName $ValueName -ValueType $ValueType -ValueData $ValueData
            write-verbose "Successfully created the registry key"
            return 111
        }
    } catch {
        write-warning "Unable to update the registry key"
        return 1
    }
}