Public/Set-pseRegistryValueName.ps1

function Set-pseRegistryValueData {
    <#
    .SYNOPSIS
    Sets the data of a registry value in the Windows Registry.
 
    .DESCRIPTION
    The Set-pseRegistryValueData function sets the data of a registry value in the Windows Registry.
 
    .PARAMETER KeyPath
    Specifies the registry key path where the value should be modified. This parameter is mandatory.
 
    .PARAMETER ValueName
    Specifies the name of the value to be modified. This parameter is mandatory.
 
    .PARAMETER ValueData
    Specifies the new data to be set for the registry value. This parameter is mandatory.
 
    .PARAMETER ValueType
    Specifies the value type to be set for the registry value. Valid options are 'String' and 'DWord'. This parameter is mandatory.
 
    .EXAMPLE
    Set-pseRegistryValueData -KeyPath "HKCU:\Software\MyApp" -ValueName "Version" -ValueData "1.0" -ValueType "String"
    This example sets the registry value named "Version" under the "HKCU:\Software\MyApp" key to the string value "1.0".
    The function returns $true if the modification is successful.
 
    .EXAMPLE
    Set-pseRegistryValueData -KeyPath "HKLM:\Software\MyApp" -ValueName "Enabled" -ValueData "1" -ValueType "DWord"
    This example sets the registry value named "Enabled" under the "HKLM:\Software\MyApp" key to the DWORD value 1.
    The function returns $true if the modification is successful.
 
    .NOTES
    Author: owen.heaume
    Version: 1.0.0 - Initial version
    #>


    [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
    )

    try {
        # Check if the registry key exists
        $keyExists = Test-Path -Path $KeyPath

        if (-not $keyExists) {
            Write-host "$KeyPath does not exist." -ForegroundColor DarkYellow
            return
        }

        # Check if the registry value exists
        $valueExists = Get-ItemProperty -Path $KeyPath -Name $ValueName -ErrorAction SilentlyContinue

        if (-not $valueExists) {
            Write-host "$ValueName not found in $KeyPath." -ForegroundColor DarkYellow
            return
        }

        # Modify the existing registry value with the new data and type
        if ($ValueType -eq 'String') {
            write-host "Setting registry to: '$keypath\$ValueName\$ValueData' of type String" -ForegroundColor DarkGray
            Set-ItemProperty -Path $KeyPath -Name $ValueName -Value $ValueData -Type String -Force | Out-Null
        }
        elseif ($ValueType -eq 'DWord') {
            write-host "Setting registry to: '$keypath\$ValueName\$ValueData' of type DWord" -ForegroundColor DarkGray
            Set-ItemProperty -Path $KeyPath -Name $ValueName -Value ([int]$ValueData) -Type DWord -Force | Out-Null
        }
    }
    catch {
        Write-Error "Failed to modify registry value: $_"
    }
}