Public/Set-UWFProtectVolume.ps1

Function Set-UWFProtectVolume {
    <#
    .SYNOPSIS
        Enables or Disables Unified Write Filter (UWF) to protect or unprotect the volume after the next system restart, if UWF is enabled after the restart.
    .DESCRIPTION
        Each volume has two entries in UWF_Volume, one for the current session and one for the next session after a restart
        You can only change the protection status of a drive for the next session
 
        UWF starts protecting the volume after the next device restart in which UWF is enabled.
 
        This method does not enable UWF if it is disabled; you must explicitly enable UWF for the next session to start volume protection.
    .PARAMETER DriveLetter
        The Driveletter
    .PARAMETER Enabled
        Enable or disable protection (Valid options are: $true or $false)
    .INPUTS
        None
    .OUTPUTS
        None
    .EXAMPLE
        Set-UWFProtectVolume -DriveLetter "C:" -Enabled $true
 
        This will enable protection for Drive C:
    .EXAMPLE
        Set-UWFProtectVolume -DriveLetter "C:" -Enabled $false
 
        This will disable protection for Drive C:
    .LINK
        about_functions_advanced
    .LINK
        about_CommonParameters
    #>

    [CmdletBinding()]
    Param(
        [String]$DriveLetter,
        [bool]$Enabled
    )

    $nextConfig = $Script:UWFVolume | Where-Object { $_.DriveLetter -eq "$DriveLetter" -and $_.CurrentSession -eq $false }

    If ($nextConfig) {
        Write-Output "Setting drive protection on $driveLetter to $enabled"
        If ($Enabled -eq $true) {
            $nextConfig.Protect() | Out-Null;
        } Else {
            $nextConfig.Unprotect() | Out-Null;
        }
    } Else {
        Throw "Error: Could not find $driveLetter. Protection is not enabled."
    }
}