LLI Wiki — My Digital Knowledge Base

Windows: force GPO update and reset local policy

gpupdate/gpresult commands and a procedure to remove local policy settings and related registry keys.

Commands to force Group Policy (GPO) processing on Windows, and a procedure to remove previously applied local policy settings.

Diagnostics and update

Command Purpose
sconfig Command-line configuration (Windows Server Core)
gpupdate /force Reapply all policies
gpresult /v Show detailed results of applied policies
rsop.msc Resultant Set of Policy (graphical console)
repadmin Active Directory replication diagnostics
gpupdate /force
gpresult /v

Remove local policy settings

Back up the files before deleting them. To remove all previously defined policy settings, delete:

%systemroot%\system32\GroupPolicy\User\registry.pol
%systemroot%\system32\GroupPolicy\Machine\registry.pol

Also delete the keys under the following paths (from the command line if regedit is locked):

HKCU\Software\Policies
HKLM\Software\Microsoft\Windows\CurrentVersion\Policies

Then reboot: the configured policy settings are removed. Note that GroupPolicy is a hidden folder.

Cleanup script

The script below removes browser policies (Chrome, Edge, Firefox) and Windows Update policies, re-enables OneDrive and deletes the local policy folders. It assumes the following registry values are behind the restrictions:

Function Write-Log {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$False)]
        [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
        [String]$Level = "INFO",

        [Parameter(Mandatory=$True)]
        [string]$Message,

        [Parameter(Mandatory=$False)]
        [string]$logfile
    )

    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
    $Line = "$Stamp $Level $Message"
    If ($logfile) {
        Add-Content $logfile -Value $Line
    }
    Else {
        $fc = $host.UI.RawUI.ForegroundColor
        switch ($Level) {
            "INFO"  { $host.UI.RawUI.ForegroundColor = "Green";  break }
            "WARN"  { $host.UI.RawUI.ForegroundColor = "Yellow"; break }
            "ERROR" { $host.UI.RawUI.ForegroundColor = "Red";    break }
            "FATAL" { $host.UI.RawUI.ForegroundColor = "Red";    break }
            "DEBUG" { "";                                        break }
        }
        Write-Output $Line
        $host.UI.RawUI.ForegroundColor = $fc
    }
}

Clear-Host
Push-Location   # Set-Location on registry hives changes the current location

Try {
    Write-Host "0. managed browser..."
    Set-Location -Path 'HKCU:\Software\'
    Remove-Item -Path HKCU:\Software\Policies\Google -Recurse
    Remove-Item -Path HKCU:\Software\Policies\Microsoft\Edge -Recurse
    Remove-Item -Path HKCU:\Software\Policies\Mozilla -Recurse

    Set-Location -Path 'HKLM:\Software\'
    Remove-Item -Path HKLM:\Software\Policies\Google -Recurse
    Remove-Item -Path HKLM:\Software\Policies\Microsoft\Edge -Recurse
    Remove-Item -Path HKLM:\Software\Policies\Mozilla -Recurse
} catch {
    Write-Host "Caught an exception: $($_.Exception.Message)" -ForegroundColor Red
    Write-Log -Level "ERROR" -Message "Exception: $($_.Exception)"
}

Try {
    Write-Host "1. Stopping Windows Update services..."
    Stop-Service -Name BITS
    Stop-Service -Name wuauserv
    Stop-Service -Name appidsvc
    Stop-Service -Name cryptsvc
    Remove-Item -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate -Recurse
    Write-Host "1b. Starting Windows Update services..."
    Start-Service -Name BITS
    Start-Service -Name wuauserv
    Start-Service -Name appidsvc
    Start-Service -Name cryptsvc
} catch {
    Write-Host "Caught an exception: $($_.Exception.Message)" -ForegroundColor Red
    Write-Log -Level "ERROR" -Message "Exception: $($_.Exception)"
}

try {
    Write-Host "2. OneDrive enabled..."
    Set-Location -Path 'HKLM:\Software\'
    Set-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\OneDrive' -Name 'DisableFileSync' -Value '0'
    Set-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\OneDrive' -Name 'DisableFileSyncNGSC' -Value '0'
} catch {
    Write-Host "Caught an exception: $($_.Exception.Message)" -ForegroundColor Red
    Write-Log -Level "ERROR" -Message "Exception: $($_.Exception)"
}

try {
    Write-Host "3. Local policy files..."
    Remove-Item C:\Windows\System32\GroupPolicy\      -Filter "*" -Recurse -Force  # -WhatIf
    Remove-Item C:\Windows\System32\GroupPolicyUsers\ -Filter "*" -Recurse -Force  # -WhatIf
} catch {
    Write-Host "Caught an exception: $($_.Exception.Message)" -ForegroundColor Red
    Write-Log -Level "ERROR" -Message "Exception: $($_.Exception)"
}

Pop-Location   # back to the original location

Notes

Mastodon LLIMastodon SocialFosstodon