Public/Set-specRegistryKeyAndValue.ps1
function Set-SpecRegistryKeyAndValue { <# .SYNOPSIS Sets or updates a registry key and value. .DESCRIPTION The Set-SpecRegistryKeyAndValue function ensures that a specified registry key exists and sets or updates its associated value. If the registry key does not exist, it will be created. The value can be updated with the provided data. The value type can be either `String` or `DWord`. .PARAMETER KeyPath The full path to the registry key. This is a mandatory parameter. .PARAMETER ValueName The name of the registry value to set or update. This is a mandatory parameter. .PARAMETER ValueData The data to assign to the registry value. This is a mandatory parameter. .PARAMETER ValueType Specifies the type of the registry value. Valid options are 'String' or 'DWord'. This is a mandatory parameter. .EXAMPLE Set-SpecRegistryKeyAndValue -KeyPath 'HKCU:\Software\MyApp' -ValueName 'Setting1' -ValueData 'Enabled' -ValueType 'String' Sets or updates a String value named 'Setting1' with data 'Enabled' under the registry key 'HKCU:\Software\MyApp'. .EXAMPLE Set-SpecRegistryKeyAndValue -KeyPath 'HKLM:\Software\MyApp' -ValueName 'MaxUsers' -ValueData '150' -ValueType 'DWord' Sets or updates a DWord value named 'MaxUsers' with data '150' under the registry key 'HKLM:\Software\MyApp'. .EXAMPLE # Define an array of custom objects representing registry key/value pairs $registryItems = @( [PSCustomObject]@{ KeyPath = 'HKCU:\Software\MyApp'; ValueName = 'Setting1'; ValueData = 'Enabled'; ValueType = 'String' }, [PSCustomObject]@{ KeyPath = 'HKCU:\Software\MyApp'; ValueName = 'MaxUsers'; ValueData = '150'; ValueType = 'DWord' }, [PSCustomObject]@{ KeyPath = 'HKLM:\Software\AnotherApp'; ValueName = 'InstallPath'; ValueData = 'C:\Program Files\AnotherApp'; ValueType = 'String' } ) # Pipe the array of custom objects into the function $registryItems | Set-SpecRegistryKeyAndValue Sends the array of custom objects to the Set-SpecRegistryKeyAndValue function to set or update the registry key/value pairs. Any errors that occur are displayed in the console and the function continues processing the remaining items. .EXAMPLE try { set-SpecRegistryKeyAndValue -KeyPath 'HKLM:\SOFTWARE\OHTesting' -ValueName 'owen1' -ValueData 'test3' -ValueType String -ea stop } catch { Write-Host "An error occurred! $_" -ForegroundColor Magenta } Attempts to set or update a registry key and value. If an error occurs, the error is caught and displayed in the console. .NOTES Author: owen.heaume Version: 1.0.0 - Initial release #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [String]$KeyPath, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [String]$ValueName, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [String]$ValueData, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [ValidateSet('String', 'DWord')] [String]$ValueType ) Begin { Write-Host 'Starting Set-SpecRegistryKeyAndValue function' } Process { Foreach ($key in $KeyPath) { Write-Host "Processing key path: $key" -ForegroundColor DarkCyan try { if (!(Test-Path -Path $key -ErrorAction Stop)) { Write-Host "Key path does not exist. Creating key: $key" -ForegroundColor DarkGray New-Item -Path $key -Force -ErrorAction Stop | Out-Null } else { Write-Host "Key path exists: $key. Skipping creation." -ForegroundColor DarkGray } } catch { Write-Error "Failed to create $key. $_" continue } try { # Set or update the registry value if ($ValueType -eq 'String') { Set-ItemProperty -Path $key -Name $ValueName -Value $ValueData -Type String -Force | Out-Null } elseif ($ValueType -eq 'DWord') { Set-ItemProperty -Path $key -Name $ValueName -Value [int]$ValueData -Type DWord -Force | Out-Null } Write-Host "Value $ValueName updated with data $ValueData" -ForegroundColor DarkGray } catch { Write-Error "An error occurred updating the value $ValueName with data $ValueData. $_" } } } End { Write-Host 'Completed processing: Set-SpecRegistryKeyAndValue' -ForegroundColor DarkGreen } } |