Skip to content
Snippets Groups Projects
Commit cc4c525f authored by Andreas Lindemark's avatar Andreas Lindemark
Browse files

New script to uninstall Chocolatey

parent 7cfed6c9
No related branches found
No related tags found
No related merge requests found
# 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment