Public/Remove-UWFRegistryValue.ps1

Function Remove-UWFRegistryValue {
    <#
    .SYNOPSIS
        Deletes the specified registry key or registry value and commits the deletion.
    .DESCRIPTION
        Deletes the specified registry key or registry value and commits the deletion.
 
        If ValueName is specified, this method will delete only the value specified by ValueName that is contained by RegistryKey. If ValueName is empty, the entire RegistryKey and all its sub keys are deleted.
 
        This method deletes the registry key or registry value from both the overlay and the persistent storage.
 
        You must use an administrator account to change any properties or call any methods that change the configuration settings.
    .PARAMETER RegistryKey
        A string that contains the full path of the registry key that contains the value to be deleted. If ValueName is empty, the entire registry key is deleted.
    .PARAMETER ValueName
        A string that contains the name of the value to be deleted.
    .INPUTS
        System.String
    .OUTPUTS
        Returns an HRESULT value that indicates WMI status or a WMI error.
    .EXAMPLE
        Remove-UWFRegistryValue
    .LINK
        about_functions_advanced
    .LINK
        about_CommonParameters
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Medium"
    )]
    Param(
        [Parameter(
            Mandatory = $true,
            HelpMessage = "A string that contains the full path of the registry key to be committed."
        )]
        [String]$RegistryKey,
        [String]$ValueName
    )

    Begin {
        If (-not $PSBoundParameters.ContainsKey('Verbose')) {
            $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
        }
        If (-not $PSBoundParameters.ContainsKey('WhatIf')) {
            $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
        }
        If (-not $PSBoundParameters.ContainsKey('Confirm')) {
            $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
        }
        If (-not $PSBoundParameters.ContainsKey('ErrorAction')) {
            $ErrorActionPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ErrorActionPreference')
        }
    }

    Process {
        If ($PSCmdlet.ShouldProcess("RegistryKey: $RegistryKey, ValueName $ValueName", "Delete from registry")) {
            If (!$Script:UWFRegistryFilter) {
                $ExitCode = 424
                Throw "Unable to get handle to an instance of the UWF_RegistryFilter class"
            }
            $CommitRegistryDeletion = $Script:UWFRegistryFilter.CommitRegistryDeletion($RegistryKey, $ValueName)
            $ExitCode = $CommitRegistryDeletion.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            If ($null -eq $ValueName) {
                Write-Output "$RegistryKey has been removed from registry"
            } Else {
                Write-Output "$ValueName in $RegistryKey has been removed from registry"
            }
        }
        Return $("{0:x0}" -f $ExitCode)
    }
}