Set-RegistryShouldBe.ps1
19 Sep 2025Description
Purpose
No synopsis provided.
Detailed Description
No detailed description provided.
Usage
No usage examples provided.
Notes
No additional notes.
Script
# Utility Function: Registry.ShouldBe
## This function is used to ensure that a registry value exists and is set to a specific value.
function Set-RegistryShouldBe {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Path,
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[string]$Value,
[Parameter(Mandatory)]
[ValidateSet('String', 'ExpandString', 'Binary', 'DWord', 'MultiString', 'QWord')]
[string]$Type
)
begin {
# Make sure the registry path exists.
if (!(Test-Path $Path)) {
Write-Warning ("Registry path '$Path' does not exist. Creating.")
New-Item -Path $Path -Force | Out-Null
}
# Make sure it's actually a registry path.
if (!(Get-Item $Path).PSProvider.Name -eq 'Registry' -and !(Get-Item $Path).PSIsContainer) {
throw "Path '$Path' is not a registry path."
}
}
process {
do {
# Make sure the registry value exists.
if (!(Get-ItemProperty -Path $Path -Name $Name -ErrorAction SilentlyContinue)) {
Write-Warning ("Registry value '$Name' in path '$Path' does not exist. Setting to '$Value'.")
New-ItemProperty -Path $Path -Name $Name -Value $Value -Force | Out-Null
}
# Make sure the registry value is correct.
if ((Get-ItemProperty -Path $Path -Name $Name).$Name -ne $Value) {
Write-Warning ("Registry value '$Name' in path '$Path' is not correct. Setting to '$Value'.")
Set-ItemProperty -Path $Path -Name $Name -Value $Value
}
} while ((Get-ItemProperty -Path $Path -Name $Name).$Name -ne $Value)
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.