Public/Remove-SpecRegistryValue.ps1

function Remove-SpecRegistryValue {
    <#
    .SYNOPSIS
    Removes a specified registry value.
 
    .DESCRIPTION
    The Remove-SpecRegistryValue function removes a specified value from a registry key if both exist. If the registry key or value does not exist, the function will log the appropriate message and continue processing.
 
    .PARAMETER KeyPath
    The full path to the registry key. This is a mandatory parameter.
 
    .PARAMETER ValueName
    The name of the registry value to remove. This is a mandatory parameter.
 
    .EXAMPLE
    Remove-SpecRegistryValue -KeyPath 'HKCU:\Software\MyApp' -ValueName 'Setting1'
    Removes the value 'Setting1' from the registry key 'HKCU:\Software\MyApp'.
 
    .EXAMPLE
    Remove-SpecRegistryValue -KeyPath 'HKLM:\Software\MyApp' -ValueName 'MaxUsers'
    Removes the value 'MaxUsers' from the registry key 'HKLM:\Software\MyApp'.
 
    .EXAMPLE
    # Define an array of custom objects representing registry key/value pairs for removal
    $registryItems = @(
        [PSCustomObject]@{ KeyPath = 'HKCU:\Software\MyApp'; ValueName = 'Setting1' },
        [PSCustomObject]@{ KeyPath = 'HKCU:\Software\MyApp'; ValueName = 'MaxUsers' },
        [PSCustomObject]@{ KeyPath = 'HKLM:\Software\AnotherApp'; ValueName = 'InstallPath' }
    )
 
    # Pipe the array of custom objects into the function
    $registryItems | Remove-SpecRegistryValue
    Sends the array of custom objects to the Remove-SpecRegistryValue 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 {
        Remove-SpecRegistryValue -KeyPath 'HKLM:\Software\MyApp' -ValueName 'MaxUsers' -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 - Update code to take into account value not existing
    #>



    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [String]$KeyPath,

        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
        [String]$ValueName
    )

    Begin {
        Write-Host 'Starting Remove-SpecRegistryValue function' -ForegroundColor DarkCyan
    }

    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 exists: $key. Checking for value $ValueName" -ForegroundColor DarkGray

                    try {
                        # Attempt to remove the registry value
                        if (Get-ItemProperty -Path $key -Name $valuename -ea SilentlyContinue) {
                            Remove-ItemProperty -Path $key -Name $ValueName -ErrorAction Stop
                            Write-Host "Value $ValueName removed successfully from $key" -ForegroundColor DarkGray
                        } else {
                            Write-Host "Unable to remove '$valuename' as it does not exist" -ForegroundColor DarkYellow
                        }
                    } catch {
                        Write-Error "Failed to remove value $ValueName from $key. $_"
                    }
                } else {
                    Write-Host "Key path does not exist: $key" -ForegroundColor DarkGray
                }
            } catch {
                Write-Error "Failed to process $key. $_"
            }
        }
    }

    End {
        Write-Host 'Completed processing: Remove-SpecRegistryValue' -ForegroundColor DarkGreen
    }
}