Public/Enable-UWF.ps1

Function Enable-UWF {
    <#
    .SYNOPSIS
        Enables Unified Write Filter (UWF) on the next restart.
    .DESCRIPTION
        Enables Unified Write Filter (UWF) on the next restart.
 
        You must use an administrator account to enable UWF.
 
        You must restart your device after you enable or disable UWF before the change takes effect.
 
        The first time you enable UWF on your device, UWF makes the following changes to your system to improve the performance of UWF:
 
        Paging files are disabled.
        System restore is disabled.
        SuperFetch is disabled.
        File indexing service is turned off.
        Defragmentation service is turned off.
        Fast boot is disabled.
        BCD setting bootstatuspolicy is set to ignoreallfailures.
 
        You can change these settings after you enable UWF if you want to. For example, you can move the page file location to an unprotected volume and re-enable paging files.
    .INPUTS
        None
    .OUTPUTS
        Returns an HRESULT value that indicates WMI status or a WMI error.
    .EXAMPLE
        Enable-UWF
 
        Returns 0 when successful. Otherwise, it returns error code.
    .LINK
        about_functions_advanced
    .LINK
        about_CommonParameters
    #>

    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = "Medium"
    )]
    Param()

    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($env:COMPUTERNAME, "Enable Unified Write Filter")) {
            If (!$Script:UWF) {
                $ExitCode = 424
                Throw "Unable to retrieve Unified Write Filter settings."
            }
            $Enable = $Script:UWF.Enable()
            $ExitCode = $Enable.ReturnValue
        }
    }

    End {
        If ($Null -eq $ExitCode) {
            # 424 Failed Dependency
            $ExitCode = 424
        }
        If ($ExitCode -eq 0) {
            Write-Warning "Unified Write Filter will be enabled after the next system restart."
        }
        Return $("{0:x0}" -f $ExitCode)
    }
}