Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save pleabargain/a2a5c11fc3854c4b40f7a8f7734039d2 to your computer and use it in GitHub Desktop.

Select an option

Save pleabargain/a2a5c11fc3854c4b40f7a8f7734039d2 to your computer and use it in GitHub Desktop.
this script will delete VS code from your machine using winget
<#
.SYNOPSIS
Uninstalls Visual Studio Code and removes leftover configuration/extension folders.
.DESCRIPTION
This script attempts to uninstall Visual Studio Code using winget first. If winget
is unavailable or the uninstall fails (e.g. VS Code wasn't installed via winget),
it falls back to locating the uninstaller via the Windows registry (checking both
per-user and machine-wide uninstall keys, including the 32-bit WOW6432Node hive).
After uninstalling the application, the script optionally removes leftover
configuration, cache, and extension folders that VS Code leaves behind, since a
standard uninstall does not clean these up.
By default the script runs interactively and asks for confirmation before:
- Proceeding with the uninstall at all
- Removing each category of leftover folder
Use -Force to skip all confirmation prompts (useful for automation/CI), or
-WhatIf to preview exactly what would happen without changing anything.
.PARAMETER Force
Skips all confirmation prompts. Uninstall and folder removal proceed automatically.
.PARAMETER KeepUserData
Skips removal of $env:APPDATA\Code (settings, keybindings, snippets, workspace
storage). Extensions and the app install folder are still handled normally.
Useful if you plan to reinstall and want to keep your settings.
.PARAMETER WhatIf
Shows what the script would do (uninstall command, folders to be removed)
without actually performing any changes. Standard PowerShell -WhatIf support.
.EXAMPLE
.\Uninstall-VSCode.ps1
Runs interactively, prompting for confirmation at each destructive step.
.EXAMPLE
.\Uninstall-VSCode.ps1 -Force
Uninstalls VS Code and removes all leftover folders without prompting.
.EXAMPLE
.\Uninstall-VSCode.ps1 -KeepUserData
Uninstalls VS Code but preserves your settings/snippets in %APPDATA%\Code.
.EXAMPLE
.\Uninstall-VSCode.ps1 -WhatIf
Shows what would happen without making any changes.
.NOTES
Author: (add your name if publishing under your own identity)
Requires: Windows PowerShell 5.1+ or PowerShell 7+, run as the user who
installed VS Code (or as Administrator if it was installed machine-wide).
Tested against the VS Code user-scope winget package (Microsoft.VisualStudioCode)
and its Inno Setup-based uninstaller.
This script does NOT remove:
- VS Code extensions data stored outside the standard folders below
- Settings synced via Settings Sync (those live in your Microsoft/GitHub account)
- Any project-local .vscode folders inside individual repos/workspaces
.LINK
https://github.com/microsoft/vscode
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "High")]
param(
[switch]$Force,
[switch]$KeepUserData
)
$ErrorActionPreference = "Stop"
function Write-Section {
param([string]$Text)
Write-Host "`n=== $Text ===" -ForegroundColor Magenta
}
# ---------------------------------------------------------------------------
# Startup banner + confirmation
# ---------------------------------------------------------------------------
Write-Host @"
--------------------------------------------------------
VS Code Uninstaller
--------------------------------------------------------
This script will:
1. Attempt to uninstall Visual Studio Code (winget,
falling back to the registry uninstaller)
2. Remove leftover config/cache/extension folders:
- %APPDATA%\Code
- %USERPROFILE%\.vscode
- %USERPROFILE%\.vscode-insiders
- %LOCALAPPDATA%\Programs\Microsoft VS Code
Run with -Help for full documentation (Get-Help),
-Force to skip prompts, or -WhatIf to preview only.
--------------------------------------------------------
"@ -ForegroundColor Cyan
if (-not $Force -and -not $WhatIfPreference) {
$response = Read-Host "Proceed with uninstalling VS Code and cleaning up its folders? (y/N)"
if ($response -notmatch '^[Yy]') {
Write-Host "Aborted by user. No changes were made." -ForegroundColor Yellow
exit 0
}
}
# ---------------------------------------------------------------------------
# Step 1: Uninstall via winget, fall back to registry uninstaller
# ---------------------------------------------------------------------------
Write-Section "Step 1: Uninstalling VS Code"
$uninstalled = $false
if (Get-Command winget -ErrorAction SilentlyContinue) {
if ($PSCmdlet.ShouldProcess("Microsoft.VisualStudioCode", "winget uninstall")) {
try {
Write-Host "Attempting to uninstall VS Code via winget..." -ForegroundColor Cyan
winget uninstall --id Microsoft.VisualStudioCode --silent --accept-source-agreements
if ($LASTEXITCODE -eq 0) {
Write-Host "VS Code uninstalled via winget." -ForegroundColor Green
$uninstalled = $true
}
else {
Write-Host "winget returned exit code $LASTEXITCODE. Falling back to registry lookup." -ForegroundColor Yellow
}
}
catch {
Write-Host "winget uninstall failed: $($_.Exception.Message)" -ForegroundColor Yellow
}
}
}
else {
Write-Host "winget not found on this system. Falling back to registry lookup." -ForegroundColor Yellow
}
if (-not $uninstalled) {
Write-Host "Searching registry for VS Code uninstaller..." -ForegroundColor Cyan
$uninstallKeys = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
)
$vscodeEntry = Get-ItemProperty $uninstallKeys -ErrorAction SilentlyContinue |
Where-Object { $_.DisplayName -like "*Visual Studio Code*" } |
Select-Object -First 1
if ($vscodeEntry -and $vscodeEntry.UninstallString) {
Write-Host "Found uninstaller: $($vscodeEntry.UninstallString)" -ForegroundColor Cyan
$uninstallString = $vscodeEntry.UninstallString -replace '"', ''
if ($PSCmdlet.ShouldProcess($uninstallString, "Run uninstaller")) {
try {
Start-Process -FilePath $uninstallString -ArgumentList "/SILENT" -Wait
Write-Host "VS Code uninstalled via registry uninstaller." -ForegroundColor Green
$uninstalled = $true
}
catch {
Write-Host "Registry uninstaller failed: $($_.Exception.Message)" -ForegroundColor Red
}
}
}
else {
Write-Host "Could not find VS Code uninstaller automatically. You may need to remove it manually via Settings > Apps." -ForegroundColor Red
}
}
# ---------------------------------------------------------------------------
# Step 2: Clean up leftover folders
# ---------------------------------------------------------------------------
Write-Section "Step 2: Cleaning up leftover folders"
$foldersToRemove = [ordered]@{
"$env:APPDATA\Code" = "Settings, keybindings, snippets, workspace storage"
"$env:USERPROFILE\.vscode" = "Installed extensions and extension data"
"$env:USERPROFILE\.vscode-insiders" = "Insiders build extensions and data"
"$env:LOCALAPPDATA\Programs\Microsoft VS Code" = "Application install folder (user-scope installs)"
}
foreach ($folder in $foldersToRemove.Keys) {
if ($KeepUserData -and $folder -eq "$env:APPDATA\Code") {
Write-Host "Skipped (KeepUserData set): $folder" -ForegroundColor DarkYellow
continue
}
if (Test-Path $folder) {
$description = $foldersToRemove[$folder]
if ($Force -or $PSCmdlet.ShouldProcess($folder, "Remove folder ($description)")) {
try {
Remove-Item -Path $folder -Recurse -Force
Write-Host "Removed: $folder" -ForegroundColor Green
}
catch {
Write-Host "Failed to remove $folder`: $($_.Exception.Message)" -ForegroundColor Red
}
}
}
else {
Write-Host "Not found (skipped): $folder" -ForegroundColor DarkGray
}
}
Write-Section "Done"
Write-Host "VS Code and its associated folders have been processed. Review any red/yellow messages above for items needing manual attention." -ForegroundColor Cyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment