Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 34 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Windows](https://img.shields.io/badge/Windows-10%2F11-green)
![License: MIT](https://img.shields.io/badge/License-MIT-yellow)

This repository contains PowerShell scripts and a batch wrapper designed to help maintain shared school laptops. It automates common cleanup and maintenance tasks so devices stay fast, reliable, and ready for students. You can choose between a **fully automated edition** or an **interactive edition** that prompts step‑by‑step.
This repository contains PowerShell scripts and a batch wrapper designed to help maintain shared school laptops. It automates common cleanup and maintenance tasks so devices stay fast, reliable, and ready for students. The main script now supports both **automatic** and **interactive** flows from a single entry point.

---

Expand All @@ -25,53 +25,48 @@ This repository contains PowerShell scripts and a batch wrapper designed to help

## 📂 Files

- `SchoolLaptopCleanup.ps1` → **Automated edition** (runs all tasks in sequence, supports `-DryRun` and `-DefragPasses`).
- `SchoolLaptopCleanup-Interactive.ps1` → **Interactive edition** (asks Y/N for each step, validates defrag passes 1–6).
- `RunCleanup.bat` → Batch wrapper that downloads the latest script from GitHub and runs it safely in PowerShell.
- `SchoolLaptopCleanup.ps1` → **Unified edition** (automatic by default; use `-Mode Manual` for interactive prompts). Supports centralized logging via `-ServerPath` and validated defrag passes with `-DefragPasses`.
- `RunCleanup.bat` → Batch wrapper that downloads the latest script from GitHub and runs it safely in PowerShell.

---

## 🚀 Usage

### Option 1: Run Automated Edition
1. Download or clone this repo.
2. Open **PowerShell as Administrator**.
3. Run:
Set-ExecutionPolicy Bypass -Scope Process -Force
.\SchoolLaptopCleanup.ps1
1. Download or clone this repo.
2. Open **PowerShell as Administrator**.
3. Run:
```powershell
Set-ExecutionPolicy Bypass -Scope Process -Force
.\SchoolLaptopCleanup.ps1
```

### Option 2: Run Interactive Edition
1. Download or clone this repo.
2. Open **PowerShell as Administrator**.
3. Run:
Set-ExecutionPolicy Bypass -Scope Process -Force
.\SchoolLaptopCleanup-Interactive.ps1
1. Download or clone this repo.
2. Open **PowerShell as Administrator**.
3. Run:
```powershell
Set-ExecutionPolicy Bypass -Scope Process -Force
.\SchoolLaptopCleanup.ps1 -Mode Manual
```

You will be prompted step‑by‑step (Y/N) for each task, and asked how many defrag passes to run (1–6).

### Option 3: Run via Batch Wrapper
1. Download or clone this repo.
2. Double‑click `RunCleanup.bat`.
- It will attempt to download the latest script from GitHub.
- If download fails, it falls back to the local copy.

---

## ⚙️ Dry Run Mode (Automated Edition)

To preview actions without making changes, run:
.\SchoolLaptopCleanup.ps1 -DryRun

This will log what *would* happen without deleting profiles or running cleanup tasks.
1. Download or clone this repo.
2. Double‑click `RunCleanup.bat`.
- It will attempt to download the latest script from GitHub.
- If an older download is found, the wrapper strips the legacy `[CmdletBinding()]` line (including BOM/whitespace variations) before execution to prevent parser errors. The sanitization now matches both `[CmdletBinding]` and `[CmdletBinding()]` forms.
- If download fails, it falls back to the local copy.

---

## 💽 Defrag Passes (Automated Edition)

By default, the automated script runs **3 passes** of defrag. You can override this with the `-DefragPasses` parameter:
By default, the automated script runs **3 passes** of defrag. You can override this with the `-DefragPasses` parameter (validated 1–6):

- Run with 3 passes (default):
.\SchoolLaptopCleanup.ps1
- Run with 3 passes (default):
.\SchoolLaptopCleanup.ps1

- Run with 5 passes:
.\SchoolLaptopCleanup.ps1 -DefragPasses 5
Expand All @@ -80,6 +75,15 @@ The interactive edition will ask you how many passes you want (1–6). Invalid i

---

## 📒 Logging

- Logs are stored in `C:\Temp\CleanupLog.txt` by default.
- Specify a central UNC path (e.g., `-ServerPath \\pat-fs1\c$`) to log to a network share (logs go in `LaptopLogs\<HOSTNAME>`).
- If the log grows beyond 5MB, it is automatically archived with a timestamp.
- Driver reports are saved as `DriverReport.csv` alongside the selected log path.

---

## 🧹 Disk Cleanup Setup

Before using the script, configure Disk Cleanup options once manually:
Expand All @@ -89,14 +93,6 @@ Select the cleanup options you want. After this, the script can run them automat

---

## 📒 Logging

- Logs are stored in `C:\Temp\CleanupLog.txt`.
- If the log grows beyond 5MB, it is automatically archived with a timestamp.
- Driver reports are saved as `DriverReport.csv` in the same folder.

---

## ⏰ Optional Automation

You can schedule the **automated edition** to run weekly using Task Scheduler:
Expand Down
11 changes: 9 additions & 2 deletions RunCleanup.bat
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ set "ScriptPath=%TEMP%\SchoolLaptopCleanup.ps1"
:: Define the local script path relative to the batch file location
set "LocalScriptPath=%~dp0SchoolLaptopCleanup.ps1"

:: Attempt to download the script using a single-line PowerShell command with the CORRECT URL
powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13; try { Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/PinkyCodeMaster/SchoolLaptopCleanup/refs/heads/main/SchoolLaptopCleanup.ps1' -OutFile '%ScriptPath%' -ErrorAction Stop; Write-Host 'Download successful.'; exit 0; } catch { Write-Host 'Download failed, checking for local script...'; exit 1; } "
:: Always clear any existing download so stale copies cannot run
if exist "%ScriptPath%" del "%ScriptPath%" >nul 2>&1

:: Attempt to download the script using the canonical raw URL
powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13; try { Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/PinkyCodeMaster/SchoolLaptopCleanup/main/SchoolLaptopCleanup.ps1' -OutFile '%ScriptPath%' -ErrorAction Stop; Write-Host 'Download successful.'; exit 0; } catch { Write-Host 'Download failed, checking for local script...'; exit 1; } "

:: Check the ERRORLEVEL set by the PowerShell command (0 for success, 1 for failure)
if %ERRORlevel% EQU 0 (
echo Running downloaded script from %TEMP%...
:: Strip legacy CmdletBinding attribute that caused parser errors in older downloads (handles BOM/whitespace variants)
powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "try { $raw = Get-Content -Path '%ScriptPath%' -Raw; $sanitized = $raw -replace '(?im)^[\uFEFF\s]*\[CmdletBinding(?:\(\))?\]\s*\r?\n', ''; if ($sanitized -ne $raw) { Set-Content -Path '%ScriptPath%' -Value $sanitized -Encoding UTF8; Write-Host 'Removed legacy CmdletBinding attribute from downloaded script.' } } catch { Write-Host 'Warning: Could not sanitize downloaded script - ' + $_.Exception.Message }"
powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%ScriptPath%"
) else (
echo Attempting to run local script...
if exist "%LocalScriptPath%" (
echo Running local script from same folder: %LocalScriptPath%
:: Sanitize local copy as well in case it still has the legacy header
powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "try { $raw = Get-Content -Path '%LocalScriptPath%' -Raw; $sanitized = $raw -replace '(?im)^[\uFEFF\s]*\[CmdletBinding(?:\(\))?\]\s*\r?\n', ''; if ($sanitized -ne $raw) { Set-Content -Path '%LocalScriptPath%' -Value $sanitized -Encoding UTF8; Write-Host 'Removed legacy CmdletBinding attribute from local script.' } } catch { Write-Host 'Warning: Could not sanitize local script - ' + $_.Exception.Message }"
powershell.exe -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%LocalScriptPath%"
) else (
echo.
Expand Down
Loading