Public/Set-RegistryValue.ps1
function Set-RegistryValue { <# .DESCRIPTION Create or modify a registry value .PARAMETER RegKey Specify the registry key path .PARAMETER Name Specify the name of the registry value to modify .PARAMETER PropertyType Specify the type of the registry value .PARAMETER Value Specify the data the value will be set to .EXAMPLE Create or modify a registry value Set-RegistryValue -RegKey "HKLM:\SOFTWARE\Example" -Name "ExampleValue" -PropertyType Dword -Value 2 Create of modify a default registry value Set-RegistryValue -RegKey "HKLM:\SOFTWARE\Example" -Name '(Default)' -PropertyType String -Value "abcd" .NOTES Created by: Jon Anderson Modified: 2023-07-10 #> [CmdletBinding()] param( [parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] [String]$RegKey, [parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] [String]$Name, [parameter(Mandatory=$true)][ValidateSet('String','ExpandString','Binary','DWord','MultiString','Qword','Unknown')] [String]$PropertyType, [parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] [String]$Value ) Write-LogEntry -Value "Create new registry key: $RegKey\$Name with value: $Value" -Severity 1 if(!(Test-Path $RegKey)) { try { New-Item -Path $RegKey -Force | Out-Null } catch { Write-LogEntry -Value "Failed to create $RegKey`n$($PSItem.Exception.Message)" -Severity 3 } } try { New-ItemProperty -Path $RegKey -Name $Name -PropertyType $PropertyType -Value $Value -Force | Out-Null } catch { Write-LogEntry -Value "Failed to set $RegKey\$Name to $Value`n$($PSItem.Exception.Message)" -Severity 3 } if((Get-ItemProperty -Path $RegKey).Name -eq $Value) { Write-LogEntry -Value "Successfully set $RegKey\$Name to $Value" -Severity 1 } } |