Public/New-specRegistryKeyAndValue.ps1
function New-SpecRegistryKeyAndValue { <# .SYNOPSIS Creates a new registry key and value if they do not already exist. .DESCRIPTION The New-SpecRegistryKeyAndValue function ensures that a specified registry key and its associated value exist. If the registry key does not exist, it will be created. If the value does not exist under the specified key, it will be created 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 create or verify. 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 New-SpecRegistryKeyAndValue -KeyPath 'HKCU:\Software\MyApp' -ValueName 'Setting1' -ValueData 'Enabled' -ValueType 'String' Creates a new String value named 'Setting1' with data 'Enabled' under the registry key 'HKCU:\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 = '100'; 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 | New-SpecRegistryKeyAndValue .EXAMPLE try { New-SpecRegistryKeyAndValue -KeyPath 'HKLM:\SOFTWARE\OHTesting' -ValueName 'owen1' -ValueData 'test3' -ValueType String -ea stop } catch { Write-Host "An error occurred! $_" -ForegroundColor Magenta } Attempts to create a new 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 - 1.0.1 - Fix issue when creating DWord values - 1.0.2 - Allow blank ValueData #> [CmdletBinding()] param ( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [String]$KeyPath, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [String]$ValueName, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)] [String]$ValueData, [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [ValidateSet('String', 'DWord')] [String]$ValueType ) Begin { Write-Host 'Starting New-SpecRegistryKeyAndValue function' -ForegroundColor DarkCyan } Process { Foreach ($key in $KeyPath) { $continue = $true Write-Host "Processing key path: $key" -ForegroundColor DarkCyan try { if (!(Test-Path -Path $key -ea 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 = $false } if ($continue) { try { # Attempt to get the property, this will fail if the value does not exist $existingValue = Get-ItemProperty -Path $key -Name $ValueName -ErrorAction Stop Write-Host "Value name exists: $ValueName with data $($existingValue.$ValueName). Skipping creation." -ForegroundColor DarkGray } catch { Write-Host "Value name does not exist. Creating value: $ValueName" -ForegroundColor DarkGray try { # Set the registry value with correct type if ($ValueType -eq 'String') { New-ItemProperty -Path $key -Name $ValueName -Value $ValueData -PropertyType 'String' -Force -ea stop | Out-Null } elseif ($ValueType -eq 'DWord') { # Ensure ValueData is cast to an unsigned 32-bit integer for DWord $intValueData = [uint32]$ValueData New-ItemProperty -Path $key -Name $ValueName -Value $intValueData -PropertyType 'DWord' -Force -ea stop | Out-Null } } catch { Write-Error "An error occurred creating the value $ValueName with data $ValueData. $_" } } } } } End { Write-Host 'Completed processing: New-SpecRegistryKeyAndValue' -ForegroundColor DarkGreen } } |