Files
Scripting/Powershell/Registry Key Search And Remove.ps1
2021-07-06 13:05:15 +10:00

217 lines
6.7 KiB
PowerShell

param([switch]$Elevated)
function Write-ProgressHelper {
param (
[int]$StepNumber,
[string]$Message
)
Write-Progress -Activity 'Microsoft Registry Fix' -Status $Message -PercentComplete (($StepNumber / $steps) * 100)
}
$script:steps = ([System.Management.Automation.PsParser]::Tokenize((gc "$PSScriptRoot\$($MyInvocation.MyCommand.Name)"), [ref]$null) | where { $_.Type -eq 'Command' -and $_.Content -eq 'Write-ProgressHelper' }).Count
$stepCounter = 0
function Check-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Check-Admin) -eq $false) {
if ($elevated)
{
# could not elevate, quit
}
else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
Write-ProgressHelper -Message 'Firing Up The Mouse Wheel' -StepNumber ($stepCounter++)
Write-Host "Please Reboot After This Is Complete For The Changes To Work" -ForegroundColor Red
Start-Sleep -Seconds 3
Write-ProgressHelper -Message 'Applying Environment Variables... For Efficiency.' -StepNumber ($stepCounter++)
$ThisPC = ($env:COMPUTERNAME + $env:username)
$mytime = Get-Date -Format "dd-MM-yyyy--HH-mm"
$workingdir = "c:\regexport"
$key1 = "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{00020827-0000-0000-C000-000000000046}"
$key2 = "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{65235197-874B-4A07-BDC5-E65EA825B718}"
$key3 = "HKLM:\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{84F66100-FF7C-4fb4-B0C0-02CD7FB668FE}"
$export1 = "HKLM\SOFTWARE\Classes\CLSID\{00020827-0000-0000-C000-000000000046}"
$export2 = "HKLM\SOFTWARE\Classes\CLSID\{65235197-874B-4A07-BDC5-E65EA825B718}"
$export3 = "HKLM\SOFTWARE\Classes\CLSID\{84F66100-FF7C-4fb4-B0C0-02CD7FB668FE}"
$regcheck = "
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Stopping For Coffee..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Write-Host " "
"
Write-ProgressHelper -Message 'Checking For Existance Of Previous Runs & Also Alien Life Forms' -StepNumber ($stepCounter++)
Start-Sleep -Seconds 3
If (Test-Path -Path c:\regexport) {
Write-Host " "
Write-Host " "
Write-Host "Directory Already Exists - But That's Cool - No Stress"
$question2 = Read-Host "It Looks Like You Have Run This Tool Previously. Do You Want Delete Previously Exported Keys (y/n)?"
if ($question2 -eq 'y') {
Write-Host "Let's Continue Then" -ForegroundColor Green
Del $workingdir\*.* -Force
}
else
{
Write-Host "Maybe Next Time" -ForegroundColor Red
[Environment]::Exit(0)
}
}
Else {
mkdir c:\regexport
}
Write-ProgressHelper -Message 'Now We Are Checking Registry Entries That Apparently Cause Issues - This Is Hard Work!' -StepNumber ($stepCounter++)
Start-Sleep -Seconds 3
if (Test-Path -Path $key1) {
Reg export $export1 c:\regexport\Key1-$ThisPC-$mytime.reg
Write-Host "Key1 Exists & Exported For Probing"
}
Else {
Write-Host "Nope, That Key Does Not Exist. On To Key Number 2"
}
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Stopping For Energy Drink..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Write-Host " "
If (Test-Path -Path $key2) {
Reg export $export2 c:\regexport\Key2-$ThisPC-$mytime.reg
Write-Host "Key2 Exists & Exported For Probing"
}
Else {
Write-Host "Nope, That Key Does Not Exist. On To Key Number 3"
}
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Stopping For A Cup of Tea & TimTams..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Start-Sleep -Seconds 1
Write-Host "...Checking Registry Settings..." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " "
Write-Host " "
If (Test-Path -Path $key3) {
Reg export $export3 c:\regexport\Key3-$ThisPC-$mytime.reg
Write-Host "Key3 Exists & Exported For Probing"
}
Else {
Write-Host "Nope, That Key Does Not Exist Either."
}
Write-ProgressHelper -Message 'Backing Up Keys - You Know... For Just-In-Case' -StepNumber ($stepCounter++)
Start-Sleep -Seconds 3
If (Test-Path -Path $workingdir\key*.reg) {
Compress-Archive -Path $workingdir\*.reg -DestinationPath $workingdir\$ThisPC.zip -CompressionLevel Optimal -Force
Write-ProgressHelper -Message 'Removing Problem Keys' -StepNumber ($stepCounter++)
Remove-Item -Path $key1 -Force -Recurse
Remove-Item -Path $key2 -Force -Recurse
Remove-Item -Path $key3 -Force -Recurse
Write-Host " "
Write-ProgressHelper -Message 'Reboot Is Now Required - It Is Annoying, But Necessary. Please Reboot When You Have A Chance.' -StepNumber ($stepCounter++)
Write-Host " "
Write-Host " "
Write-Host "All Done - Have A Fantastic Day - Be Well!"
Start-Sleep -Seconds 5
Write-Host "Rebooting in...." -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " 5" -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " 4" -ForegroundColor Green
Start-Sleep -Seconds 1
Write-Host " 3" -ForegroundColor Yellow
Start-Sleep -Seconds 1
Write-Host " 2" -ForegroundColor Yellow
Start-Sleep -Seconds 1
Write-Host " 1" -ForegroundColor Red
Start-Sleep -Seconds 1
powershell ./exit.ps1
[Environment]::Exit(0)
}
else {
Write-ProgressHelper -Message 'No Problem Keys Found - Please Contact Your Friendly Neighbourhood IT Person For Further Assistance' -StepNumber ($stepCounter++)
Start-Sleep -Seconds 5
powershell ./exit.ps1
[Environment]::Exit(0)
}