Functions/registry.ps1


function Set-RegistryValue(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string]$regPath,

    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateNotNullOrEmpty()]
    [string]$regName,

    [Parameter(Mandatory = $true, Position = 2)]
    [ValidateNotNullOrEmpty()]
    [string]$regValue,

    [Parameter(Position = 3)]
    [string] $logDisplayName = "Registry",

    [Parameter(Position = 4)]
    [ValidateSet("none", "append", "overwrite")]
    [string] $overwriteType = "none",

    [Parameter(Position = 5)]
    [Microsoft.Win32.RegistryValueKind] $type = [Microsoft.Win32.RegistryValueKind]::String
) {
    $result = Get-RegistryValue $regPath $regName
    if (-not $result) {
        Write-Warning "${logDisplayName}: Creating Key '${regName}' value '${regValue}' type: '${type}'"
        Set-ItemProperty -Path $regPath -Name ${regName} -Value $regValue -Type $type
    }
    elseif ($overwriteType -eq "overwrite") {
        Write-Warning "${logDisplayName}: Overwriting Key '${regName}' value '${regValue}' / overwriteType: '${overwriteType}' type: '${type}'"
        Set-ItemProperty -Path $regPath -Name ${regName} -Value $regValue -Type $type
    }
    elseif ($overwriteType -eq "append") {
        $newValue = "${result};${regValue}"
        Write-Warning "${logDisplayName}: Modifying Key '${regName}' value '${newValue}' / overwriteType: '${overwriteType}' type: '${type}'"
        Set-ItemProperty -Path $regPath -Name ${regName} -Value $newValue -Type $type        
    }
    else {
        Write-Warning "${logDisplayName}: Key '${regName}' already exists with value '${result}'"
    }
}

# Gets the specified registry value or $null if it is missing
function Get-RegistryValue(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string] $path,
    
    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateNotNullOrEmpty()]
    [string] $regName) {
    #return Get-ItemPropertyValue -Path $path -Name $regName -ErrorAction SilentlyContinue
    $key = Get-Item -LiteralPath $path -ErrorAction SilentlyContinue
    if ($key) {
        $key.GetValue($regName, $null)
    }
}

$envCurrentUserPath = "HKCU:\Environment"
$envLocalMachinePath = "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
function Set-EnvironmentVar(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string]$regName,

    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateNotNullOrEmpty()]
    [string]$regValue,

    [Parameter(Position = 2)]
    [ValidateSet("user", "machine")]
    [string]
    # environment variable type: user, or machine. Default is 'user'
    $variableType = 'user'
) {
    $environmentPath = $envCurrentUserPath
    if ($variableType -ceq 'machine') {
        $environmentPath = $envLocalMachinePath
    }
    Set-RegistryValue $environmentPath $regName $regValue "Environment Variable"
}

function Get-EnvironmentVar(
    [Parameter(Mandatory = $true)]
    [ValidateNotNullOrEmpty()]
    [string] $regName,

    [ValidateSet("user", "machine")]
    [string]
    # environment variable type: user, or machine. Default is 'user'
    $variableType = 'user'
) {
    $environmentPath = $envCurrentUserPath
    if ($variableType -ceq 'machine') {
        $environmentPath = $envLocalMachinePath
    }
    return Get-RegistryValue $environmentPath $regName
}

function Set-LocalMachineRunOnceVar(
    [Parameter(Mandatory = $true, Position = 0)]
    [ValidateNotNullOrEmpty()]
    [string]$regName,

    [Parameter(Mandatory = $true, Position = 1)]
    [ValidateNotNullOrEmpty()]
    [string]$regValue
) {
    $localMachineRunOncePath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
    Set-RegistryValue $localMachineRunOncePath $regName $regValue "LocalMachine: RunOnce Variable"
}