# Usage:
#   Open PowerShell as Administrator
#     win+x
#     alt+a
#     alt+y
#   Run:
#     In powershell Terminal:
#       iwr -UseBasicParsing -Uri "https://git.thecoven.info/PrincessPi/general-scripts-and-system-ssssssetup/raw/branch/main/Windows-Scripts/windows-repair.ps1?nocache=$(Get-Random)" -OutFile "$env:TEMP\windows-repair-temp.ps1"; powershell -ExecutionPolicy Bypass -File "$env:TEMP\windows-repair-temp.ps1"

# check if hasadmin rights, if notm run script in new terminal, clopsing old
## if not, carry on
if (-not ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544')) {
    # launch tghis script int new window after promopting for admin
    Start-Process -FilePath 'powershell' -Verb RunAs -ArgumentList "-ExecutionPolicy Bypass -File $PSCommandPath"

    # CLOSE previous terminal
    exit 0
} else {
    Write-Host "`nFIXING WINDOWS FULL STYLE`n"

    Write-Host "ABORTING ANY SCHEDULED SHUTDOWN"
    shutdown.exe /a 1> $null # cmd abort scheduled shutdowns

    Write-Host "GENERATING AND DISPLAYING COMPREHENSIVE STABILITY REPORT"
    Start-Process "perfmon.exe" -ArgumentList "/report" -Wait

    Write-Host "GENERATING AND DISPLAYING STABILITY HISTORY"
    Start-Process "perfmon.exe" -ArgumentList "/rel" -Wait

    Write-Host "CLEARING DNS"
    ipconfig.exe /flushsdns 1> $null 

    Write-Host "CLEARING ARP CACHE"
    Remove-NetNeighbor -Confirm:$false 1> $null 

    Write-Host "CLEARING ALL SAMBA CREDENTIALS"
    net.exe use * /delete /y 1> $null # nuke all samba creds

    Write-Host "RENEWING DHCP"
    # release and renew dhcp
    ipconfig.exe /release 1> $null 
    ipconfig.exe /renew 1> $null 

    Write-Host "UPDATING MALWARE SIGNATURES"
    Update-MpSignature # update windows defender malware siggs

    Write-Host "RUNNING WINDOWS MALICIOUS SOFTWARE REMOVAL TOOL (MRT, MAY TAKE A LONG TIME, WONT SHOW STATUS)"
    # Start-Process -FilePath "MRT.exe" -ArgumentList "/F:Y /Q" -Wait # do full microsoft malicious software removal scan in background automatically removing anything found, wait to proceed

    Write-Host "UPDATING MALWARE SIGNATURES (AGAIN)"
    Update-MpSignature # update windows defender malware siggs

    Write-Host "RUNNING FULL WINDOWS DEFENDER SCAN"
    Start-MpScan -ScanType FullScan # full windows defender scan

    Write-Host "RUNNING DISM ONLINE IMAGE CLEANUP"
    DISM.exe /Online /Cleanup-Image /RestoreHealth # online check for bad files

    Write-Host "RUNNING SYSTEM FILE CHECKER (SFC)"
    SFC.exe /scannow # older check for bad files

    Write-Host "SCHEDULING ESSENTIAL FILE CHECK ON NEXT BOOT"
    SFC.exe /scanonce # check essential files on next boot

    Write-Host "SCHEDULING OFFLINE CHECK DISK AND REPAIR OF C: (CHKDSK)"
    echo y | chkdsk.exe /f /r C: # cmd checks C drive after reboot to waste time and fix errors (noninteractive via y ffuckery)

    Write-Host "SCHEDULINMG OFFLINE MEMTEST"
    mdsched.exe /s # schedule offline memtest (noninteractive)

    Write-Host "SCHEDULING WINDOWS DEFENDER OFFLINE SCAN MAY REBOOT UNEXPECTEDLY"
    Start-MpWDOScan # powershell starts Windows Defender Offline Scan after reboot

    Write-Host "`nREBOOTING IN 5 MINUTES MAX PROVBABLY SOONER`n"
    shutdown.exe /r /t (60*5) # shutdown in 5 minutes as failsafe
}