Public/Add-UWFVolumeExclusion.ps1

Function Add-UWFVolumeExclusion {
    <#
        .SYNOPSIS
            Adds a file or folder to the file exclusion list for a volume protected by Unified Write Filter (UWF).
        .DESCRIPTION
            Adds a file or folder to the file exclusion list for a volume protected by Unified Write Filter (UWF).
 
            You must use an administrator account to add or remove file or folder exclusions during run time, and you must restart the device for new exclusions to take effect.
 
            Important
 
            You can’t add exclusions for the following items:
 
            The volume root. For example, C: or D:.
            The \Windows folder on the system volume.
            The \Windows\System32 folder on the system volume.
            The \Windows\system32\drivers folder on the system volume.
            Paging files.
 
            However, you can exclude subdirectories and files under these items.
        .PARAMETER FileName
            A string that contains the full path of the file or folder relative to the volume.
        .INPUTS
            System.String
        .OUTPUTS
            Returns an HRESULT value that indicates WMI status or a WMI error.
        .EXAMPLE
            Add-UWFVolumeExclusion -FileName "C:\Operator"
        .LINK
            Remove-UWFVolumeExclusion
        .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 file or folder relative to the volume."
        )]
        [String]$FileName
    )

    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($FileName, "Exclude from UWF")) {
            If (!$Script:UWFVolume) {
                $ExitCode = 424
                Throw "Unable to get handle to an instance of the UWF_Volume class"
            }
            $AddExclusion = $Script:UWFVolume.AddExclusion($FileName)
            $ExitCode = $AddExclusion.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            Write-Warning "Adding file and folder exclusion for $FileName on the next restart"
        }
        Return $("{0:x0}" -f $ExitCode)
    }
}