# Remediation logic

This page describes the overall remediation logic and the primary remediation actions executed by the script.

This list is intended as a quick reference. Each function includes an inline comment block in the script describing behaviour, safety considerations and edge cases. When writing tests or adapting the script, prefer calling these helpers rather than re-implementing enumerators or retry logic.

  • Overview
  • Remediation Script Logic
    • CleanMgr Cleanup
    • Storage Sense Cleanup
    • DISM Component Store Cleanup
    • User-Specific Temporary File Cleanup
    • Event Log Cleanup
    • Old User Profile Cleanup
    • Logging
    • Disk Space Accounting
  • Notes for testers and automation

# Overview

This page covers the remediation flow used by Win-Storage-Remediate.ps1. Each major action is described with safety notes and references to helper functions.

# Remediation Script Logic

The remediation script performs the following actions. Each major action below is given a short subsection heading to provide stable anchors for the documentation and Intune parameter links.

# CleanMgr Cleanup

  • Prepares selected CleanMgr VolumeCaches by setting StateFlags0001 in the registry.
  • Suppresses the Disk Cleanup GUI to ensure silent execution.
  • Validates the cleanmgr.exe path before execution.
  • Logs the process exit code.

# Storage Sense Cleanup

  • Configures and triggers Storage Sense to remove temporary files and locally cached OneDrive content.
  • Uses Set-StorageSense and Start-StorageSense cmdlets where available.

# DISM Component Store Cleanup

  • Runs DISM with /Online /Cleanup-Image /StartComponentCleanup to clean up the WinSxS component store.
  • Logs the process exit code and respects pending-reboot detection.

# User-Specific Temporary File Cleanup

  • Removes files from %USERPROFILE%\\AppData\\Local\\Temp, leaving folders expressly excluded by the script.
  • Implements retry logic for file access conflicts and respects $MaxCleanupDurationMinutes time budgets.

# Event Log Cleanup

  • Exports selected logs to an archive directory ($env:TEMP\\EventLogArchives) using wevtutil epl.
  • Clears selected logs using wevtutil cl after successful export.

# Old User Profile Cleanup

  • Removes user profiles under C:\Users that are not whitelisted (Default, Default User, Public, All Users) and have not been used in the last $userProfileRetentionDays days.

# Logging

  • Centralised logging with Start-Transcript and Write-Log.
  • Temporary logs are consolidated into the main log file at the end of the script; archives are created per-run.

# Disk Space Accounting

  • Captures initial free space on C: before remediation.
  • Captures final free space after remediation and calculates the recovered space.

# Notes for testers and automation

  • Prefer -DryRun for initial tests.
  • Use -CleanMgrOnly to test CleanMgr preparation without further changes.
  • Use -SKIP_SLOW_IO on slow VMs to avoid long enumeration passes.