Public/Remove-UWFRegistryExclusion.ps1

Function Remove-UWFRegistryExclusion {
    <#
    .SYNOPSIS
        Removes a registry key from the registry exclusion list for Unified Write Filter (UWF).
    .DESCRIPTION
        Removes a registry key from the registry exclusion list for Unified Write Filter (UWF).
 
        You must restart the device before the registry key is excluded from UWF filtering.
    .PARAMETER RegistryKey
        A string that contains the full path of the registry key.
    .INPUTS
        System.String
    .OUTPUTS
        Returns an HRESULT value that indicates WMI status or a WMI error.
    .EXAMPLE
        Remove-UWFRegistryExclusion -RegistryKey "HKLM\SOFTWARE"
    .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."
        )]
        [String]$RegistryKey
    )

    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, "Remove from exclusion list")) {
            If (!$Script:UWFRegistryFilter) {
                $ExitCode = 424
                Throw "Unable to get handle to an instance of the UWF_RegistryFilter class"
            }
            $RemoveExclusion = $Script:UWFRegistryFilter.RemoveExclusion($RegistryKey)
            $ExitCode = $RemoveExclusion.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            Write-Warning "$RegistryKey will be removed from the exclusion list after reboot"
        } Else {
            Return $("{0:x0}" -f $ExitCode)
        }
    }
}