Skip to content

Instantly share code, notes, and snippets.

@wjz1095
Created August 2, 2022 15:12
Show Gist options
  • Save wjz1095/9edaf565f6e03a29b3c9119cab356e6b to your computer and use it in GitHub Desktop.
Save wjz1095/9edaf565f6e03a29b3c9119cab356e6b to your computer and use it in GitHub Desktop.
Fix Windows - Powershell (Admin)
#Run this script as admin, auto-elevate if not ran as admin
Write-Host "[INFO] Elevating script to admin..."
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
Pause
Write-Host "[INFO] Start time:" -ForegroundColor Yellow
$StartTime = Get-Date
Write-Host $StartTime
Write-Host "[FILE] Flushing DNS cache..." -ForegroundColor Yellow
ipconfig /flushdns
Write-Host "[FILE] Clearing ARP cache..." -ForegroundColor Yellow
arp -d *
netsh interface ip delete arpcache
Write-Host "[FILE] Clearing temp files...": -ForegroundColor Yellow
Set-Location -Path "C:\Windows\Temp"
Write-Host "[FILE] Flushing C:\Windows\Temp"
Remove-Item * -Force -Recurse -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
Set-Location -Path $env:TEMP
Write-Host "[FILE] Flushing AppData Temp"
Remove-Item * -Force -Recurse -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
Sleep 2
Write-Host "[INFO] Beginning DISM scans..." -ForegroundColor Yellow
Write-Host "[SCAN] Starting Scan 1 of 3"
Write-Host "[INFO] If scan gets stuck, press ENTER" -ForegroundColor Yellow
DISM /Online /Cleanup-Image /CheckHealth
Write-Host "[SCAN] Scan 1 complete"
Write-Host " "
Write-Host "[SCAN] Starting Scan 2 of 3"
Write-Host "[INFO] If scan gets stuck, press ENTER" -ForegroundColor Yellow
DISM /Online /Cleanup-Image /ScanHealth
Write-Host "[SCAN] Scan 2 complete"
Write-Host " "
Write-Host "[SCAN] Starting Scan 3 of 3"
Write-Host "[INFO] If scan gets stuck, press ENTER" -ForegroundColor Yellow
DISM /Online /Cleanup-Image /RestoreHealth
Write-Host "[SCAN] Scan 3 complete"
Sleep 2
Write-Host "[SCAN] Running SFC scan..." -ForegroundColor Yellow
SFC /scannow
Write-Host "[INFO] Opening Disk Cleanup..." -ForegroundColor Yellow
cleanmgr.exe
Sleep 1
Write-Host "[INFO] Opening defragmenter..." -ForegroundColor Yellow
dfrgui
Write-Host "[WARN] Scanning C: drive on next boot!!!" -ForegroundColor Red
chkdsk /f /r c:
$EndTime = Get-Date
Write-Host "End time:"
Write-Host $Endtime
$RunTime = New-TimeSpan -Start $StartTime -End (get-date)
"[INFO] Execution time was {0}:{1}:{2}" -f $RunTime.Hours, $RunTime.Minutes, $RunTime.Seconds
Write-Host "###########ATTENTION###########" -ForegroundColor Red
Write-Host "A scan is set to run on the next boot, please restart your computer and allow it to scan" -ForegroundColor Red
Write-Host ""
Write-Host "[INFO] Showing system stats..." -ForegroundColor Yellow
systeminfo | findstr /B /C:"OS Name" /C:"OS Version" /C:"Original Install Date:" /C:"Time Zone" /C:"Total Physical Memory"
Write-Host ""
$bootuptime = (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime
$CurrentDate = Get-Date
$uptime = $CurrentDate - $bootuptime
Write-Host "[INFO] System uptime" -ForegroundColor Yellow
Write-Host "$($uptime.days) days, $($uptime.Hours) hours, $($uptime.Minutes) minutes"
Write-Host ""
Write-Host "Perhaps consider a reboot? :)" -Foreground Cyan
Pause
Write-Host "Pressing [Enter] again will close this window"
Pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment