Skip to content

Instantly share code, notes, and snippets.

@minanagehsalalma
Created April 11, 2025 01:16
Show Gist options
  • Save minanagehsalalma/12b5ac7f3ed26a56fd9dc8ac59490149 to your computer and use it in GitHub Desktop.
Save minanagehsalalma/12b5ac7f3ed26a56fd9dc8ac59490149 to your computer and use it in GitHub Desktop.
WhatsApp Syncing Chats 1% Stuck fix with no data loss nor resetting (a full troubleshooting suite)
# WhatsApp Sync Fix Script Suite
# Purpose: Fix WhatsApp syncing issues without losing messages
# Compatible with: WhatsApp from Microsoft Store on Windows 10
<#
.SYNOPSIS
A comprehensive script suite to fix WhatsApp synchronization issues on Windows 10.
.DESCRIPTION
This script performs several actions to fix WhatsApp syncing problems:
- Terminates WhatsApp processes
- Clears the WhatsApp cache
- Repairs WhatsApp app storage
- Checks network connectivity
- Resets Windows Store cache
- Verifies and repairs app dependencies
- Restarts WhatsApp with optimized settings
.NOTES
Author: bee booob
Date: April 11, 2025
Requires: Windows 10, PowerShell 5.1 or later, Administrator privileges
#>
#Requires -RunAsAdministrator
#Requires -Version 5.1
# Set strict mode to catch errors
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Show-Menu {
Clear-Host
Write-Host "========================================"
Write-Host " WHATSAPP SYNC ISSUE REPAIR TOOL "
Write-Host "========================================"
Write-Host ""
Write-Host "1. Run Complete Fix (Recommended)"
Write-Host "2. Kill WhatsApp Processes"
Write-Host "3. Clear WhatsApp Cache"
Write-Host "4. Repair WhatsApp App Data"
Write-Host "5. Check Network Connectivity"
Write-Host "6. Reset Microsoft Store"
Write-Host "7. Re-register WhatsApp App"
Write-Host "8. Exit"
Write-Host ""
Write-Host "NOTE: Your messages will be preserved with all options"
Write-Host ""
}
function Kill-WhatsAppProcesses {
Write-Host "Terminating all WhatsApp processes..." -ForegroundColor Yellow
# Get all WhatsApp related processes
$processes = Get-Process | Where-Object {
$_.ProcessName -like "*WhatsApp*" -or
$_.ProcessName -eq "WhatsApp" -or
$_.ProcessName -eq "WhatsAppDesktop" -or
$_.MainWindowTitle -like "*WhatsApp*"
}
# Terminate each process
foreach ($process in $processes) {
try {
Stop-Process -Id $process.Id -Force
Write-Host "Terminated process: $($process.ProcessName) (ID: $($process.Id))" -ForegroundColor Green
}
catch {
Write-Host "Failed to terminate process $($process.ProcessName): $_" -ForegroundColor Red
}
}
# Additional check for any remaining WhatsApp background processes
$remainingProcesses = Get-Process | Where-Object { $_.ProcessName -like "*WhatsApp*" }
if ($remainingProcesses) {
Write-Host "Terminating remaining background processes..." -ForegroundColor Yellow
foreach ($process in $remainingProcesses) {
try {
Stop-Process -Id $process.Id -Force
Write-Host "Terminated background process: $($process.ProcessName)" -ForegroundColor Green
}
catch {
Write-Host "Failed to terminate background process $($process.ProcessName): $_" -ForegroundColor Red
}
}
}
Write-Host "All WhatsApp processes terminated." -ForegroundColor Green
Start-Sleep -Seconds 2
}
function Clear-WhatsAppCache {
Write-Host "Clearing WhatsApp cache files..." -ForegroundColor Yellow
# Get current user's AppData paths
$localAppData = [Environment]::GetFolderPath('LocalApplicationData')
$roamingAppData = [Environment]::GetFolderPath('ApplicationData')
# Define cache paths (Microsoft Store version of WhatsApp)
$cachePaths = @(
# Main WhatsApp cache locations
"$localAppData\Packages\*WhatsApp*\LocalCache\Roaming\WhatsApp\*Cache*",
"$localAppData\Packages\*WhatsApp*\LocalState\*Cache*",
"$localAppData\Packages\*WhatsApp*\TempState",
# Electron/Chromium cache for WhatsApp
"$localAppData\Packages\*WhatsApp*\LocalCache\Roaming\WhatsApp\Cache",
"$localAppData\Packages\*WhatsApp*\LocalCache\Roaming\WhatsApp\Code Cache",
"$localAppData\Packages\*WhatsApp*\LocalCache\Roaming\WhatsApp\GPUCache",
# Additional temporary data
"$localAppData\Packages\*WhatsApp*\LocalCache\Roaming\WhatsApp\Temp",
"$localAppData\Packages\*WhatsApp*\LocalState\Temp"
)
# Important locations to preserve (to avoid clearing messages)
$preservePaths = @(
"$localAppData\Packages\*WhatsApp*\LocalState\*\Databases",
"$localAppData\Packages\*WhatsApp*\LocalCache\Roaming\WhatsApp\Local Storage",
"$localAppData\Packages\*WhatsApp*\LocalState\*\Logs"
)
# Process each cache path
foreach ($path in $cachePaths) {
$items = Get-Item -Path $path -ErrorAction SilentlyContinue
if ($items) {
foreach ($item in $items) {
# Skip if this is a path we want to preserve
$shouldPreserve = $false
foreach ($preservePath in $preservePaths) {
if ($item.FullName -like $preservePath) {
$shouldPreserve = $true
break
}
}
if (-not $shouldPreserve) {
try {
if (Test-Path $item.FullName) {
if ($item -is [System.IO.DirectoryInfo]) {
# For directories, remove contents but not the directory itself
Get-ChildItem -Path $item.FullName -Recurse -Force | Remove-Item -Force -Recurse -ErrorAction SilentlyContinue
Write-Host "Cleared cache directory: $($item.FullName)" -ForegroundColor Green
}
else {
# For files, remove the file
Remove-Item -Path $item.FullName -Force -ErrorAction SilentlyContinue
Write-Host "Removed cache file: $($item.FullName)" -ForegroundColor Green
}
}
}
catch {
Write-Host "Failed to clear cache item $($item.FullName): $_" -ForegroundColor Red
}
}
else {
Write-Host "Preserved data directory: $($item.FullName)" -ForegroundColor Cyan
}
}
}
}
# Clear specific problematic files that might cause sync issues
$syncFiles = Get-ChildItem -Path "$localAppData\Packages\*WhatsApp*" -Recurse -Filter "*sync*" -File -ErrorAction SilentlyContinue
foreach ($file in $syncFiles) {
# Skip files in the preserved paths
$shouldPreserve = $false
foreach ($preservePath in $preservePaths) {
if ($file.FullName -like $preservePath) {
$shouldPreserve = $true
break
}
}
if (-not $shouldPreserve) {
try {
Remove-Item -Path $file.FullName -Force -ErrorAction SilentlyContinue
Write-Host "Removed sync state file: $($file.FullName)" -ForegroundColor Green
}
catch {
Write-Host "Failed to remove sync file $($file.FullName): $_" -ForegroundColor Red
}
}
}
Write-Host "WhatsApp cache clearing complete." -ForegroundColor Green
Start-Sleep -Seconds 2
}
function Repair-WhatsAppAppData {
Write-Host "Repairing WhatsApp app data..." -ForegroundColor Yellow
# Get WhatsApp package full name
$whatsAppPackage = Get-AppxPackage | Where-Object { $_.Name -like "*WhatsApp*" }
if ($whatsAppPackage) {
try {
# Run the built-in repair tool
Write-Host "Found WhatsApp package: $($whatsAppPackage.PackageFullName)" -ForegroundColor Green
Write-Host "Running repair operation..." -ForegroundColor Yellow
# Use PowerShell to repair the app
$null = Repair-AppxPackage -Package $whatsAppPackage.PackageFullName
Write-Host "WhatsApp app data repair completed successfully." -ForegroundColor Green
}
catch {
Write-Host "Failed to repair WhatsApp app data: $_" -ForegroundColor Red
Write-Host "Attempting alternative repair method..." -ForegroundColor Yellow
try {
# Try the reset method as an alternative
Reset-AppxPackage -Online -PackageName $whatsAppPackage.PackageFullName
Write-Host "Alternative repair method completed." -ForegroundColor Green
}
catch {
Write-Host "Alternative repair method failed: $_" -ForegroundColor Red
}
}
}
else {
Write-Host "WhatsApp package not found. Please ensure WhatsApp is installed from the Microsoft Store." -ForegroundColor Red
}
Start-Sleep -Seconds 2
}
function Check-NetworkConnectivity {
Write-Host "Checking network connectivity..." -ForegroundColor Yellow
# Define important WhatsApp service endpoints
$endpoints = @(
"web.whatsapp.com",
"www.whatsapp.com",
"static.whatsapp.net",
"graph.whatsapp.com",
"chat.cdn.whatsapp.net",
"pps.whatsapp.net"
)
# Check general internet connectivity
try {
$internetTest = Test-NetConnection -ComputerName "www.google.com" -InformationLevel Quiet
if ($internetTest) {
Write-Host "Internet connection is working properly." -ForegroundColor Green
}
else {
Write-Host "WARNING: Internet connection appears to be down!" -ForegroundColor Red
Write-Host "Please check your network connection and try again." -ForegroundColor Red
return $false
}
}
catch {
Write-Host "Failed to check internet connectivity: $_" -ForegroundColor Red
return $false
}
# Check WhatsApp specific endpoints
$allEndpointsAccessible = $true
foreach ($endpoint in $endpoints) {
try {
$result = Test-NetConnection -ComputerName $endpoint -InformationLevel Quiet -ErrorAction SilentlyContinue
if ($result) {
Write-Host "Successfully connected to $endpoint" -ForegroundColor Green
}
else {
Write-Host "WARNING: Cannot connect to $endpoint" -ForegroundColor Red
$allEndpointsAccessible = $false
}
}
catch {
Write-Host "Failed to connect to $endpoint: $_" -ForegroundColor Red
$allEndpointsAccessible = $false
}
}
# Get network adapter information and check for potential issues
try {
$networkAdapters = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }
foreach ($adapter in $networkAdapters) {
Write-Host "Active network adapter: $($adapter.Name) (Status: $($adapter.Status))" -ForegroundColor Cyan
# Check for common network issues
if ($adapter.MediaConnectionState -ne "Connected") {
Write-Host "WARNING: Network adapter $($adapter.Name) reports connection state: $($adapter.MediaConnectionState)" -ForegroundColor Red
}
}
}
catch {
Write-Host "Failed to get network adapter information: $_" -ForegroundColor Red
}
if (-not $allEndpointsAccessible) {
Write-Host ""
Write-Host "Network connectivity check complete with WARNINGS." -ForegroundColor Yellow
Write-Host "Some WhatsApp service endpoints may be inaccessible." -ForegroundColor Yellow
Write-Host "This could be due to network restrictions, proxy settings, or firewall rules." -ForegroundColor Yellow
return $false
}
else {
Write-Host ""
Write-Host "Network connectivity check complete. All WhatsApp endpoints are accessible." -ForegroundColor Green
return $true
}
Start-Sleep -Seconds 2
}
function Reset-MicrosoftStore {
Write-Host "Resetting Microsoft Store cache..." -ForegroundColor Yellow
try {
# Reset the Windows Store cache
Write-Host "Stopping Windows Store Service..." -ForegroundColor Yellow
Stop-Service -Name "AppXSvc" -Force -ErrorAction SilentlyContinue
# Clear store cache
Write-Host "Clearing Microsoft Store cache..." -ForegroundColor Yellow
$null = Start-Process -FilePath "WSReset.exe" -Wait
Write-Host "Microsoft Store cache reset completed." -ForegroundColor Green
# Re-register Microsoft Store
Write-Host "Re-registering Microsoft Store..." -ForegroundColor Yellow
try {
$null = powershell.exe -ExecutionPolicy Bypass -Command "& {Get-AppXPackage -AllUsers | Where-Object {`$_.InstallLocation -like '*Microsoft.WindowsStore*'} | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "`$(`$_.InstallLocation)\AppXManifest.xml"}}"
Write-Host "Microsoft Store re-registered successfully." -ForegroundColor Green
}
catch {
Write-Host "Failed to re-register Microsoft Store: $_" -ForegroundColor Red
}
}
catch {
Write-Host "Failed to reset Microsoft Store: $_" -ForegroundColor Red
}
Start-Sleep -Seconds 2
}
function Reregister-WhatsAppApp {
Write-Host "Re-registering WhatsApp app..." -ForegroundColor Yellow
# Get WhatsApp package
$whatsAppPackage = Get-AppxPackage | Where-Object { $_.Name -like "*WhatsApp*" }
if ($whatsAppPackage) {
try {
# Re-register the app
Write-Host "Found WhatsApp package: $($whatsAppPackage.PackageFullName)" -ForegroundColor Green
Write-Host "Attempting to re-register..." -ForegroundColor Yellow
$manifestPath = Join-Path -Path $whatsAppPackage.InstallLocation -ChildPath "AppxManifest.xml"
if (Test-Path $manifestPath) {
Add-AppxPackage -DisableDevelopmentMode -Register $manifestPath
Write-Host "WhatsApp app re-registered successfully." -ForegroundColor Green
}
else {
Write-Host "AppxManifest.xml not found at expected location." -ForegroundColor Red
# Try a different approach - PowerShell direct re-registration
Write-Host "Trying alternative re-registration method..." -ForegroundColor Yellow
$null = powershell.exe -ExecutionPolicy Bypass -Command "& {Get-AppXPackage -AllUsers | Where-Object {`$_.Name -like '*WhatsApp*'} | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "`$(`$_.InstallLocation)\AppXManifest.xml"}}"
Write-Host "Alternative re-registration method completed." -ForegroundColor Green
}
}
catch {
Write-Host "Failed to re-register WhatsApp app: $_" -ForegroundColor Red
}
}
else {
Write-Host "WhatsApp package not found. Please ensure WhatsApp is installed from the Microsoft Store." -ForegroundColor Red
}
Start-Sleep -Seconds 2
}
function Fix-PermissionsAndRegistry {
Write-Host "Fixing WhatsApp permissions and registry entries..." -ForegroundColor Yellow
# Get package info
$whatsAppPackage = Get-AppxPackage | Where-Object { $_.Name -like "*WhatsApp*" }
if (-not $whatsAppPackage) {
Write-Host "WhatsApp package not found. Skipping permission fixes." -ForegroundColor Red
return
}
# Reset app permissions
try {
Write-Host "Resetting app permissions..." -ForegroundColor Yellow
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$localAppData = [Environment]::GetFolderPath('LocalApplicationData')
$whatsAppDataPath = "$localAppData\Packages\$($whatsAppPackage.PackageFamilyName)"
if (Test-Path $whatsAppDataPath) {
# Reset permissions on WhatsApp data folder
$acl = Get-Acl -Path $whatsAppDataPath
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($currentUser, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$acl.SetAccessRule($accessRule)
Set-Acl -Path $whatsAppDataPath -AclObject $acl
Write-Host "Reset permissions on $whatsAppDataPath" -ForegroundColor Green
}
# Check for registry issues
Write-Host "Checking for problematic registry entries..." -ForegroundColor Yellow
# Registry paths that might affect WhatsApp syncing
$registryPaths = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones"
)
foreach ($path in $registryPaths) {
if (Test-Path $path) {
Write-Host "Checking registry path: $path" -ForegroundColor Cyan
# Check for proxy settings that might interfere
if ($path -eq "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings") {
$proxyEnabled = Get-ItemProperty -Path $path -Name "ProxyEnable" -ErrorAction SilentlyContinue
if ($proxyEnabled -and $proxyEnabled.ProxyEnable -eq 1) {
Write-Host "WARNING: System proxy is enabled which might interfere with WhatsApp sync." -ForegroundColor Yellow
Write-Host "Consider temporarily disabling system proxy if sync issues persist." -ForegroundColor Yellow
}
}
}
}
Write-Host "Permission and registry check completed." -ForegroundColor Green
}
catch {
Write-Host "Failed to fix permissions: $_" -ForegroundColor Red
}
Start-Sleep -Seconds 2
}
function Restart-WhatsApp {
Write-Host "Attempting to start WhatsApp with optimized settings..." -ForegroundColor Yellow
# Get package info
$whatsAppPackage = Get-AppxPackage | Where-Object { $_.Name -like "*WhatsApp*" }
if (-not $whatsAppPackage) {
Write-Host "WhatsApp package not found. Cannot restart WhatsApp." -ForegroundColor Red
return
}
try {
# Start WhatsApp using the app execution alias
Write-Host "Starting WhatsApp..." -ForegroundColor Green
# Try to find the specific app ID for WhatsApp
$appId = (Get-StartApps | Where-Object { $_.Name -like "*WhatsApp*" }).AppID
if ($appId) {
# Use Start-Process with AppID
Start-Process "shell:AppsFolder\$appId"
Write-Host "WhatsApp started successfully." -ForegroundColor Green
}
else {
# Fallback to launching from the Start Menu
try {
Start-Process "WhatsApp"
Write-Host "WhatsApp started using process name." -ForegroundColor Green
}
catch {
# Final fallback to launching the app package directly
try {
$packageFullName = $whatsAppPackage.PackageFullName
$manifestPath = Join-Path -Path $whatsAppPackage.InstallLocation -ChildPath "AppxManifest.xml"
if (Test-Path $manifestPath) {
[xml]$manifest = Get-Content $manifestPath
$appId = $manifest.Package.Applications.Application.Id
if ($appId) {
Start-Process "shell:AppsFolder\$packageFullName!$appId"
Write-Host "WhatsApp started using package manifest information." -ForegroundColor Green
}
else {
Write-Host "Failed to find app ID in manifest." -ForegroundColor Red
}
}
else {
Write-Host "Failed to find AppxManifest.xml" -ForegroundColor Red
}
}
catch {
Write-Host "All methods to start WhatsApp failed. Please start WhatsApp manually from Start Menu." -ForegroundColor Red
}
}
}
}
catch {
Write-Host "Failed to restart WhatsApp: $_" -ForegroundColor Red
Write-Host "Please restart WhatsApp manually from the Start Menu." -ForegroundColor Yellow
}
Start-Sleep -Seconds 2
}
function Run-CompleteFix {
Write-Host "Running complete WhatsApp sync fix..." -ForegroundColor Cyan
Write-Host "This will perform all repair operations sequentially." -ForegroundColor Cyan
Write-Host ""
# Run all fix functions in optimal order
Kill-WhatsAppProcesses
Clear-WhatsAppCache
Fix-PermissionsAndRegistry
Repair-WhatsAppAppData
Check-NetworkConnectivity | Out-Null
Reset-MicrosoftStore
Reregister-WhatsAppApp
Write-Host ""
Write-Host "All repair operations completed!" -ForegroundColor Green
Write-Host ""
Write-Host "Would you like to restart WhatsApp now? (Y/N)" -ForegroundColor Yellow
$restartChoice = Read-Host
if ($restartChoice -eq "Y" -or $restartChoice -eq "y") {
Restart-WhatsApp
}
else {
Write-Host "Please restart WhatsApp manually when you're ready." -ForegroundColor Yellow
}
Write-Host ""
Write-Host "If the sync issue persists after restart, try the following additional steps:" -ForegroundColor Cyan
Write-Host "1. Restart your phone and ensure WhatsApp mobile is working properly" -ForegroundColor Cyan
Write-Host "2. Check your internet connection on both PC and phone" -ForegroundColor Cyan
Write-Host "3. Verify that your phone and PC are on the same network" -ForegroundColor Cyan
Write-Host "4. Try connecting your phone to a different network (mobile data)" -ForegroundColor Cyan
Write-Host "5. Make sure your phone's WhatsApp app is updated to the latest version" -ForegroundColor Cyan
Write-Host ""
}
# Main script execution starts here
try {
# Check for admin rights
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if (-not $isAdmin) {
Write-Host "This script requires administrator privileges to run properly." -ForegroundColor Red
Write-Host "Please restart PowerShell as Administrator and run the script again." -ForegroundColor Yellow
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
# Show menu and handle user input
$exit = $false
while (-not $exit) {
Show-Menu
$choice = Read-Host "Enter your choice (1-8)"
switch ($choice) {
"1" { Run-CompleteFix }
"2" { Kill-WhatsAppProcesses }
"3" { Clear-WhatsAppCache }
"4" { Repair-WhatsAppAppData }
"5" { Check-NetworkConnectivity | Out-Null }
"6" { Reset-MicrosoftStore }
"7" { Reregister-WhatsAppApp }
"8" { $exit = $true }
default {
Write-Host "Invalid option. Please enter a number between 1 and 8." -ForegroundColor Red
Start-Sleep -Seconds 1
}
}
if (-not $exit) {
Write-Host ""
Write-Host "Press any key to return to the menu..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
}
catch {
Write-Host ""
Write-Host "An error occurred:" -ForegroundColor Red
Write-Host $_ -ForegroundColor Red
Write-Host $_.ScriptStackTrace -ForegroundColor Red
Write-Host ""
Write-Host "Press any key to exit..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
@minanagehsalalma
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment