Skip to content

Scripts Windows Maintenance

Quadstronaut edited this page Jun 11, 2026 · 2 revisions

Scripts: Windows Maintenance

All scripts in Scripts/Windows/Maintenance/. All require administrator privileges.


Invoke-SystemUpgrade.ps1

What it does: One-command full system maintenance run.

Steps in order:

  1. Verifies administrator privileges (exits if not admin — does not auto-elevate).
  2. Installs Chocolatey if it is not already installed.
  3. Launches SFC (sfc /scannow) and DISM (dism /Online /Cleanup-Image /RestoreHealth) as background processes so they run in parallel.
  4. Saves a Chocolatey package inventory to ~/Documents/Chocolatey.txt (choco list --local-only).
  5. Upgrades all Chocolatey packages (choco upgrade all -y).
  6. Upgrades pip if pip is on the PATH.
  7. Queries Windows Update for available updates and installs them. Does not force a reboot — it reports whether a reboot may be required.

Parameters: None.

Requirements: Administrator privileges. Chocolatey is installed automatically if missing; pip and Windows Update COM objects must be accessible.

Usage:

.\Scripts\Windows\Maintenance\Invoke-SystemUpgrade.ps1

Notes:

  • SFC and DISM run in the background — they start immediately (step 3) but their windows are hidden. They may still be running when the Chocolatey and pip steps complete.
  • The inventory save uses choco list --local-only (-lo is an alias; both forms work).
  • choco upgrade all -y will restart services affected by upgraded packages. Run interactively so you can respond to any prompts Chocolatey passes through.
  • Windows Update via COM (Microsoft.Update.Session) installs updates but does not reboot. Result code 3 means "updates installed, reboot may be required."

Invoke-DiskCheck.ps1

What it does: Runs chkdsk C: /f /r on the C: drive.

  • /f — fixes filesystem errors.
  • /r — locates bad sectors and recovers readable data.

Parameters: None. Auto-elevates if not already admin.

Usage:

.\Scripts\Windows\Maintenance\Invoke-DiskCheck.ps1

Notes:

  • On a live (in-use) C: drive, chkdsk /r cannot run immediately. It schedules a check at the next system boot and prompts you to confirm.
  • The check can take a long time on large or degraded drives — expect 30–120+ minutes on boot.
  • /r implies /f.

Get-ErrorReport.ps1

What it does: Queries the Application and System Windows Event Logs for Level 2 (Error) events within a configurable lookback window, groups by source, and highlights noisy sources that exceed a configurable error threshold.

Parameters:

Parameter Type Default Description
-DaysBack int 7 How many days back to search
-ErrorThreshold int 10 Sources with more than this many errors are printed in detail

Usage:

# Default: 7 days, flag sources with >10 errors
.\Scripts\Windows\Maintenance\Get-ErrorReport.ps1

# Custom: 14 days, flag sources with >5 errors
.\Scripts\Windows\Maintenance\Get-ErrorReport.ps1 -DaysBack 14 -ErrorThreshold 5

Output: For each noisy source, prints a table of the first 10 events (TimeCreated, Id, Message). Ends with a total error count across all sources.

Requirements: Read access to the Windows Event Log. Administrator access is recommended because some log entries require elevated permissions to read.


Invoke-SfcDism.ps1

What it does: A reference script — a commented collection of the most useful sfc.exe and dism.exe commands for diagnosing and repairing Windows.

This script is not meant to be run top-to-bottom. Open it, read the commands, and run the ones you need individually.

Included commands:

SFC (System File Checker)

Command What it does
sfc /scannow Scan and repair all protected system files
sfc /verifyonly Scan without repairing
sfc /scanfile=<path> Scan and repair a single file
sfc /offwindir=... /offbootdir=... /scanfile=... Scan an offline Windows installation

DISM (Deployment Image Servicing and Management)

Command What it does
dism /online /cleanup-image /restorehealth Repair the Windows image (downloads from Windows Update if needed)
dism /online /cleanup-image /startcomponentcleanup Clean up the WinSxS folder
dism /online /cleanup-image /startcomponentcleanup /resetbase Deep cleanup — resets base, cannot be undone
dism /online /cleanup-image /analyzecomponentstore Analyze WinSxS and report potential savings
dism /online /get-packages List all installed packages and their states

Recommended repair sequence

# 1. Repair the Windows image first
dism /online /cleanup-image /restorehealth

# 2. Then use the healthy image to repair system files
sfc /scannow

Running SFC before DISM risks it sourcing repairs from an already-corrupted image. Always DISM first.

Clone this wiki locally