RepairWindows.psm1
function RepairWin { [CmdletBinding()] param () function Run-Command { param ( [string]$Command, [string]$Description ) Write-Output "Running: $Description" try { Invoke-Expression $Command Write-Output "`n$((Get-Date).ToString('u')) - Success: $Description`n" } catch { Write-Error "`n$((Get-Date).ToString('u')) - Failed: $Description`n" } } # Check if running as admin if (-not ([bool](New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) { Write-Error "This script must be run as an administrator." exit 1 } # Run DISM command Run-Command -Command "DISM /Online /Cleanup-Image /RestoreHealth" -Description "Restore health of Windows image" # Run SFC command twice for ($i = 1; $i -le 2; $i++) { Run-Command -Command "sfc /scannow" -Description "System File Checker (SFC) scan $i/2" } } |