Created
June 21, 2025 07:30
-
-
Save enonethreezed/169aed30e653975e37e2060dd8e3aa9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Write-Output "`nStarting Tools Cleanup" | |
| function Safe-RemoveItem($path) { | |
| if (!(Test-Path $path)) { return } | |
| Get-ChildItem -Path $path -Recurse -Force -ErrorAction SilentlyContinue | | |
| ForEach-Object { try { $_.IsReadOnly = $false } catch {} } | |
| $maxRetries = 5; $retry = 0 | |
| do { | |
| try { Remove-Item -Recurse -Force $path -ErrorAction Stop; return } | |
| catch { $retry++; Start-Sleep -Milliseconds 500 } | |
| } while ($retry -lt $maxRetries) | |
| } | |
| function Test-Winget { | |
| try { winget --version | Out-Null; return $true } | |
| catch { return $false } | |
| } | |
| $desktop = [Environment]::GetFolderPath('Desktop') | |
| $baseFolder = Join-Path $desktop 'tools' | |
| function Uninstall-MSIProductCode { | |
| param( | |
| [string]$displayNamePattern | |
| ) | |
| $key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall' | |
| $entry = Get-ChildItem $key | ForEach-Object { Get-ItemProperty $_.PSPath } | | |
| Where-Object { $_.DisplayName -match $displayNamePattern } | |
| if ($entry) { | |
| if ($entry.UninstallString -match '/I\{(?<code>[^}]+)\}') { | |
| $code = $Matches['code'] | |
| Write-Output "Uninstalling $($entry.DisplayName) (ProductCode: $code)..." | |
| Start-Process -FilePath 'msiexec.exe' -ArgumentList "/X{$code}", "/qn", "/norestart" -Wait | |
| Write-Output "$($entry.DisplayName) uninstalled" | |
| } else { | |
| Write-Warning "Could not parse ProductCode for $($entry.DisplayName)" | |
| } | |
| } else { | |
| Write-Output "$displayNamePattern not found" | |
| } | |
| } | |
| Write-Output "Uninstalling Winget packages..." | |
| if (Test-Winget) { | |
| $packages = @( | |
| 'Sandboxie.Classic', | |
| 'WinsiderSS.SystemInformer', | |
| 'Python.Python.3.13', | |
| 'Hashicorp.Vagrant', | |
| 'Hashicorp.Packer', | |
| 'Git.Git', | |
| 'UPX.UPX', | |
| 'Microsoft.WinDbg', | |
| 'WireGuard.WireGuard', | |
| 'OpenVPNTechnologies.OpenVPN', | |
| 'VPNetwork.TorGuard', | |
| 'Microsoft.VisualStudio.2022.Community' | |
| ) | |
| foreach ($pkg in $packages) { | |
| Write-Output "Checking $pkg..." | |
| try { | |
| $found = winget list --id $pkg 2>$null | Select-String $pkg | |
| if ($found) { | |
| Write-Output "Uninstalling $pkg..." | |
| winget uninstall --id $pkg --disable-interactivity | |
| Write-Output "$pkg uninstalled" | |
| } else { | |
| Write-Output "$pkg not installed" | |
| } | |
| } catch { | |
| Write-Warning "Error uninstalling $pkg: $($_.Exception.Message)" | |
| } | |
| } | |
| } else { | |
| Write-Warning "Winget not available; skipping Winget uninstallations." | |
| } | |
| Write-Output "Uninstalling Temurin JDK 21..." | |
| Uninstall-MSIProductCode -displayNamePattern 'Temurin.*21' | |
| Write-Output "Uninstalling Metasploit Framework..." | |
| Uninstall-MSIProductCode -displayNamePattern 'Metasploit Framework' | |
| Write-Output "Uninstalling TAP-Windows drivers..." | |
| Uninstall-MSIProductCode -displayNamePattern 'TAP-Windows' | |
| Write-Output "Removing Defender exclusions..." | |
| try { | |
| Remove-MpPreference -ExclusionPath $baseFolder -ErrorAction SilentlyContinue | |
| Remove-MpPreference -ExclusionPath 'C:\metasploit-framework' -ErrorAction SilentlyContinue | |
| Remove-MpPreference -ExclusionPath 'C:\metasploit-framework*.zip' -ErrorAction SilentlyContinue | |
| Write-Output "Defender exclusions removed" | |
| } catch { | |
| Write-Warning "Error removing Defender exclusions: $($_.Exception.Message)" | |
| } | |
| Write-Output "Removing tools directory: $baseFolder" | |
| Safe-RemoveItem $baseFolder | |
| Write-Output "All tool files removed" | |
| Write-Output "Uninstalling Visual Studio Community non-interactively..." | |
| $vsInstallerPath = Join-Path "$env:ProgramFiles(x86)\Microsoft Visual Studio\Installer" 'vs_installer.exe' | |
| if (Test-Path $vsInstallerPath) { | |
| & $vsInstallerPath uninstall --productId Microsoft.VisualStudio.Product.Community --quiet --wait --norestart | |
| Write-Output "Visual Studio Community uninstalled" | |
| } else { | |
| Write-Warning "Visual Studio Installer not found; skipping Community uninstall." | |
| } | |
| Write-Output "Refreshing environment variables..." | |
| $machinePath = [Environment]::GetEnvironmentVariable('Path','Machine') | |
| $userPath = [Environment]::GetEnvironmentVariable('Path','User') | |
| $env:Path = "$machinePath;$userPath" | |
| Write-Output "Environment refreshed." | |
| Write-Output "Cleanup complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment