Add optimize-windows.ps1 Windows optimization script - #7481
Open
HASSanHASSgit wants to merge 1 commit into
Open
Conversation
…-run default) Adds a PowerShell tool that performs safe dry-run by default and supports -Execute, -Aggressive, -Force, and -Reboot flags for real runs. Requires Administrator privileges. Logs actions and writes a manifest of deletions/terminated processes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
HASSanHASSgit
temporarily deployed
to
github-app-auth
August 3, 2026 07:11 — with
GitHub Actions
Inactive
HASSanHASSgit
temporarily deployed
to
github-app-auth
August 3, 2026 07:11 — with
GitHub Actions
Inactive
HASSanHASSgit
temporarily deployed
to
github-app-auth
August 3, 2026 07:11 — with
GitHub Actions
Inactive
HASSanHASSgit
temporarily deployed
to
github-app-auth
August 3, 2026 07:12 — with
GitHub Actions
Inactive
HASSanHASSgit
temporarily deployed
to
github-app-auth
August 3, 2026 07:13 — with
GitHub Actions
Inactive
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new administrative PowerShell utility (optimize-windows.ps1) intended to perform Windows cleanup/optimization with a dry-run default and optional execution/aggressive modes. This fits the repo as an operational/maintenance helper script rather than a runtime library change.
Changes:
- Introduces
optimize-windows.ps1with logging plus a JSON manifest of actions. - Implements temp cleanup, recycle bin emptying, process termination heuristics, optional service stopping, DISM/SFC runs, and volume optimization.
- Adds Windows Update cache clearing and optional reboot behavior when executed.
Suppressed comments (2)
optimize-windows.ps1:177
- Service stop/disable entries are currently written into the manifest's
terminatedsection. If you add a dedicatedservicessection, update this append to write there so consumers can distinguish services from processes.
if ($DoStop) {
if ($svc.Status -ne 'Stopped') {
Stop-Service -Name $svc.Name -Force -ErrorAction Stop
Set-Service -Name $svc.Name -StartupType Disabled -ErrorAction SilentlyContinue
Append-Manifest -manifestPath $manifest -section 'terminated' -entry $entry
Log "Stopped & disabled service: $($svc.Name)"
optimize-windows.ps1:127
$procInfosis expensive to compute (Get-CimInstance Win32_Process) and is never used, which adds avoidable runtime overhead for every run.
# Get process performance counters
$procInfos = Get-CimInstance Win32_Process | ForEach-Object {
$p = $_
$owner = try { ($p | Invoke-CimMethod -MethodName GetOwner).User } catch { '' }
[PSCustomObject]@{
| foreach ($it in $items) { | ||
| # Skip reparse points, system-critical files | ||
| if ($it.Attributes -band [IO.FileAttributes]::ReparsePoint) { continue } | ||
| $entry = @{Path=$it.FullName; Length=$it.Length; LastWrite=$it.LastWriteTime.ToString()} |
Comment on lines
+137
to
+160
| # Use Get-Process for CPU % via sampling | ||
| $samples = Get-Process | Select-Object Id, ProcessName, CPU, WS | ||
| foreach ($s in $samples) { | ||
| $name = $s.ProcessName | ||
| if ($whitelist -contains $name) { continue } | ||
| $wsMB = [int]($s.WS / 1MB) | ||
| $cpu = if ($s.CPU) { [int]$s.CPU } else { 0 } | ||
| $shouldKill = $false | ||
| if ($cpu -ge $CpuThreshold -or $wsMB -ge $MemMBThreshold) { $shouldKill = $true } | ||
| if ($shouldKill) { | ||
| $entry = @{Name=$name; Id=$s.Id; CPU=$cpu; WS_MB=$wsMB} | ||
| if ($DoKill) { | ||
| try { | ||
| Stop-Process -Id $s.Id -Force -ErrorAction Stop | ||
| Append-Manifest -manifestPath $manifest -section 'terminated' -entry $entry | ||
| Log "Terminated process $name (PID $($s.Id)) CPU=$cpu WS_MB=$wsMB" | ||
| } catch { | ||
| Log "Failed to terminate $name (PID $($s.Id)): $($_.Exception.Message)" | ||
| } | ||
| } else { | ||
| Log "Would terminate $name (PID $($s.Id)) CPU=$cpu WS_MB=$wsMB" | ||
| } | ||
| } | ||
| } |
Comment on lines
+63
to
+71
| function Get-TempPaths { | ||
| return @( | ||
| $env:TEMP, | ||
| $env:TMP, | ||
| "$env:SystemRoot\Temp", | ||
| "$env:SystemRoot\SoftwareDistribution\Download", | ||
| "$env:SystemRoot\Prefetch" | ||
| ) | Where-Object { $_ -and (Test-Path $_) } | Select-Object -Unique | ||
| } |
|
|
||
| # 8) Clear Windows Update cache (SoftwareDistribution) | ||
| $wuCache = "$env:SystemRoot\SoftwareDistribution\Download" | ||
| if (Test-Path $wuCache) { |
|
|
||
| function Create-BackupManifest { | ||
| $manifest = "$PSScriptRoot\optimize-backup-$(Get-Date -Format yyyyMMdd-HHmmss).json" | ||
| @{created=(Get-Date); deletions=@(); terminated=@()} | ConvertTo-Json | Out-File -FilePath $manifest -Encoding UTF8 |
| foreach ($d in $drives) { | ||
| if ($DoOptimize) { | ||
| try { | ||
| Optimize-Volume -DriveLetter $d.DriveLetter -Defrag -Verbose -ErrorAction Stop |
Comment on lines
+54
to
+60
| function Append-Manifest { | ||
| param($manifestPath, $section, $entry) | ||
| $json = Get-Content $manifestPath -Raw | ConvertFrom-Json | ||
| $list = $json.$section | ||
| $list += $entry | ||
| $json.$section = $list | ||
| $json | ConvertTo-Json | Out-File -FilePath $manifestPath -Encoding UTF8 |
Contributor
|
@HASSanHASSgit, again no issue is attached to this PR. Why do we need this change? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation & Context
This change adds a PowerShell utility to automate Windows cleanup and performance optimization. It addresses the need for a repeatable maintenance tool that safely previews actions by default and offers an aggressive execution mode for administrators who want to reclaim resources and remove unnecessary temp/cache files.
Description & Review Guide
Related Issue
N/A
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix).