From cc4c525fe331ab4028bf656aa480e9479e7fa651 Mon Sep 17 00:00:00 2001
From: Andreas Lindemark <andreas.lindemark@liu.se>
Date: Fri, 4 Apr 2025 17:32:09 +0200
Subject: [PATCH] New script to uninstall Chocolatey

---
 Windows/Chocolatey/ChocolateyUninstall.ps1 | 78 ++++++++++++++++++++++
 1 file changed, 78 insertions(+)
 create mode 100644 Windows/Chocolatey/ChocolateyUninstall.ps1

diff --git a/Windows/Chocolatey/ChocolateyUninstall.ps1 b/Windows/Chocolatey/ChocolateyUninstall.ps1
new file mode 100644
index 0000000..a4d0c7c
--- /dev/null
+++ b/Windows/Chocolatey/ChocolateyUninstall.ps1
@@ -0,0 +1,78 @@
+# Function to check if Chocolatey is installed
+function Test-ChocolateyInstalled {
+   return (Get-Command choco.exe -ErrorAction SilentlyContinue)
+}
+
+# Get the computer name
+$computerName = $env:COMPUTERNAME
+
+# Define the output file path
+$outputFilePath = ".\ChocoInstall_$computerName.ps1"
+
+# Check if Chocolatey is installed before listing packages
+if (Test-ChocolateyInstalled) {
+   # List all installed Chocolatey packages and save to the output file
+   $installedPackages = choco list --no-color | Select-String -Pattern "^[^ ]+" | Where-Object { $_ -notmatch "Chocolatey v" -and $_ -notmatch "packages installed" -and $_ -notmatch "^chocolatey" -and $_ -notmatch "^KB" } | ForEach-Object { "choco install -y " + ($_ -split ' ')[0] }
+   $installedPackages | Out-File -FilePath $outputFilePath
+} else {
+   Write-Output "Chocolatey is not installed on this system."
+}
+
+# Warning message and user confirmation
+$warningMessage = "WARNING: This script will uninstall Chocolatey and remove all related files, folders, and registry settings. Do you want to proceed? (Y/N)"
+$userResponse = Read-Host -Prompt $warningMessage
+
+if ($userResponse -eq 'Y' -or $userResponse -eq 'y') {
+   # Check if Chocolatey is installed before uninstalling chocolatey-gui
+   if (Test-ChocolateyInstalled) {
+      # Uninstall chocolatey-gui if it exists
+      if (choco list --no-color | Select-String -Pattern "chocolatey-gui") {
+         choco uninstall chocolatey-gui -y
+      }
+   }
+
+   # Remove Chocolatey from disk
+   if (Test-Path "$env:ProgramData\chocolatey") {
+      Remove-Item -Recurse -Force "$env:ProgramData\chocolatey"
+   }
+   if ($null -ne $env:ChocolateyInstall) {
+      if (Test-Path "$env:ChocolateyInstall") {
+         Remove-Item -Recurse -Force "$env:ChocolateyInstall"
+      }
+   }
+   if (Test-Path "$env:ProgramData\Chocolatey GUI") {
+      Remove-Item -Recurse -Force "$env:ProgramData\Chocolatey GUI"
+   }
+   if (Test-Path "$env:ProgramData\ChocolateyHttpCache") {
+      Remove-Item -Recurse -Force "$env:ProgramData\ChocolateyHttpCache"
+   }
+
+   # Remove Chocolatey from environment variables
+   [System.Environment]::SetEnvironmentVariable("ChocolateyInstall", $null, [System.EnvironmentVariableTarget]::Machine)
+   [System.Environment]::SetEnvironmentVariable("ChocolateyLastPathUpdate", $null, [System.EnvironmentVariableTarget]::Machine)
+
+   # Remove Chocolatey from the registry
+   if (Test-Path "HKLM:\Software\Chocolatey") {
+      Remove-Item -Path "HKLM:\Software\Chocolatey" -Recurse -Force
+   }
+   if (Test-Path "HKCU:\Software\Chocolatey") {
+      Remove-Item -Path "HKCU:\Software\Chocolatey" -Recurse -Force
+   }
+
+   # Remove Chocolatey folders from user temp directories
+   $userFolders = Get-ChildItem "C:\Users\" -Directory | Where-Object { $_.Name -notin @('All Users', 'Default', 'Default User', 'Public') }
+   foreach ($userFolder in $userFolders) {
+      $tempChocoPath = "$($userFolder.FullName)\AppData\Local\Temp\chocolatey"
+      $tempChocoScratchPath = "$($userFolder.FullName)\AppData\Local\Temp\ChocolateyScratch"
+      if (Test-Path $tempChocoPath) {
+         Remove-Item -Recurse -Force $tempChocoPath
+      }
+      if (Test-Path $tempChocoScratchPath) {
+         Remove-Item -Recurse -Force $tempChocoScratchPath
+      }
+   }
+
+   Write-Output "Chocolatey has been completely removed from this system."
+} else {
+   Write-Output "Action cancelled by user."
+}
\ No newline at end of file
-- 
GitLab