# Run DISM commands
try {
Write-Host "Running DISM /Online /Cleanup-Image /ScanHealth"
DISM.exe /Online /Cleanup-Image /ScanHealth
} catch {
Write-Host "An error occurred while running DISM /Online /Cleanup-Image /ScanHealth"
Write-Host $_.Exception.Message
}
try {
Write-Host "Running DISM /Online /Cleanup-Image /CheckHealth"
DISM.exe /Online /Cleanup-Image /CheckHealth
} catch {
Write-Host "An error occurred while running DISM /Online /Cleanup-Image /CheckHealth"
Write-Host $_.Exception.Message
}
try {
Write-Host "Running DISM /Online /Cleanup-Image /RestoreHealth"
DISM.exe /Online /Cleanup-Image /RestoreHealth
} catch {
Write-Host "An error occurred while running DISM /Online /Cleanup-Image /RestoreHealth"
Write-Host $_.Exception.Message
}
# Get all local drives
$drives = Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType = 3"
# Run chkdsk against each drive
foreach ($drive in $drives) {
$driveLetter = $drive.DeviceID
try {
# Run disk error checking using chkdsk
Write-Host "Running chkdsk for drive $driveLetter"
echo y | chkdsk $driveLetter /f /r
} catch {
Write-Host "An error occurred while checking $driveLetter"
Write-Host $_.Exception.Message
}
}
try {
# Start a component cleanup with DISM
Write-Host "Running DISM /Online /Cleanup-Image /StartComponentCleanup"
DISM.exe /Online /Cleanup-Image /StartComponentCleanup
} catch {
Write-Host "An error occurred while running DISM /Online /Cleanup-Image /StartComponentCleanup"
Write-Host $_.Exception.Message
}
try {
# Scan and fix any integrity violations in the system files
Write-Host "Running sfc /scannow"
sfc /scannow
} catch {
Write-Host "An error occurred while running sfc /scannow"
Write-Host $_.Exception.Message
}
Script data
Language - PowerShell
Run as - System / Root User
Script timeout duration - 5 Mins
Read me
Windows System Maintenance Script This script is designed to maintain and repair the integrity of a Windows system and its file systems using built-in Windows tools such as the Deployment Image Servicing and Management (DISM) tool, the Check Disk (chkdsk) utility, and the System File Checker (SFC). Features Check and Repair Windows Image Health: The script begins with DISM commands to scan, check, and restore the health of your system's Windows image. It utilizes the /ScanHealth, /CheckHealth, and /RestoreHealth parameters in that order. These commands look for any corruptions within the Windows image and attempt to repair them. Check and Repair Drives: The script then retrieves all the local drives on the system. For each drive found, it executes the chkdsk command with the /f (fix errors) and /r (locate bad sectors and recover readable information) parameters. This helps to find and rectify any potential errors or bad sectors on your drives. Windows Component Cleanup: The script uses the DISM tool with the /Online /Cleanup-Image /StartComponentCleanup command to clean up any superseded or unused system components, effectively reducing the size of the component store. System File Check and Repair: Finally, the script executes the sfc /scannow command. This scans all protected system files for corruption and automatically replaces corrupted files with a cached copy stored in a compressed folder at %WinDir%\System32\dllcache. Error Handling The script utilizes PowerShell's try-catch blocks for error handling. Each operation is enclosed in a try-catch block that captures any errors and prints out a descriptive error message. This makes it easier to troubleshoot any issues that may arise during the script's execution. Usage This script is designed to run silently in the background without requiring any user interaction, making it an excellent choice for use in regular system maintenance routines. However, please note that some of these operations can be time-consuming and may temporarily impact system performance while they are running. It is recommended to run this script during off-peak hours or times when minimal system disruption is anticipated.