Public/Add-UWFRegistryExclusion.ps1

Function Add-UWFRegistryExclusion {
    <#
    .SYNOPSIS
        Adds a registry key to the registry exclusion list for Unified Write Filter (UWF).
    .DESCRIPTION
        Adds a registry key to the registry exclusion list for Unified Write Filter (UWF).
 
        Returns an HRESULT value that indicates WMI status or a WMI error.
        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
    .EXAMPLE
        Add-UWFRegistryExclusion
    .LINK
        about_functions_advanced
    .LINK
        about_CommonParameters
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Medium"
    )]
    Param(
        [Parameter(
            Mandatory = $true,
            ValueFromPipeline = $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, "Add to exclusion list")) {
            If (!$Script:UWFRegistryFilter) {
                $ExitCode = 424
                Throw "Unable to get handle to an instance of the UWF_RegistryFilter class"
            }
            $AddExclusion = $Script:UWFRegistryFilter.AddExclusion($RegistryKey)
            $ExitCode = $AddExclusion.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            Write-Output "$Registry has been added to exclusion list"
        }
        Return $("{0:x0}" -f $ExitCode)
    }
}