A comprehensive PowerShell script to reliably and cleanly uninstall Cygwin from Windows.
The script automates the manual steps normally required to remove Cygwin, including:
- Stopping and removing Cygwin-related Windows services
- Terminating Cygwin processes
- Deleting Cygwin installation directories and package caches
- Cleaning
PATHandCYGWINenvironment variables (User + Machine) - Removing relevant registry keys
- Deleting Cygwin shortcuts from the Desktop and Start Menu
It is designed to be safe, conservative, and idempotent: you can run it multiple times without causing additional damage or unwanted side effects.
The script performs the following actions:
-
Elevation check
- Verifies that PowerShell is running with Administrator privileges.
- Exits with an error message if not elevated.
-
Cygwin service cleanup
- Scans all Windows services using WMI.
- Identifies services whose
PathNamecontainscygwin(case-insensitive). - Attempts to stop each matching service.
- Removes each matching service with
sc.exe delete.
-
Cygwin process termination
- Attempts to stop common Cygwin-related processes by name:
bash,sh,mintty,XWin,xterm,rxvt,cygserver
- Additionally scans all running processes and terminates any whose executable
Pathcontainscygwin.
- Attempts to stop common Cygwin-related processes by name:
-
Detection of Cygwin installation directories
-
Reads from the following registry locations (if present):
HKLM:\SOFTWARE\Cygwin\setupHKLM:\SOFTWARE\WOW6432Node\Cygwin\setupHKCU:\Software\Cygwin\setup
-
Uses the
rootdirvalue (when available) to determine the Cygwin installation directory. -
Also checks standard default install paths:
C:\cygwin64C:\cygwin
-
-
Detection of Cygwin package cache directories
- Reads the
last-cachevalue (if present) from the same registry keys. - Treats these as candidate Cygwin package cache locations.
- Reads the
-
Safe deletion of installation directories
For each discovered install root:
- Ensures the path:
- Exists
- Is not a drive root (e.g. not
C:\) - Contains
cygwin(case-insensitive) in its path string
- Takes ownership using
takeown.exe. - Grants full control to
Administratorsand the current user viaicacls.exe. - Deletes the directory recursively with
Remove-Item.
- Ensures the path:
-
Safe deletion of package cache directories (optional)
For each discovered cache directory:
- Ensures the path:
- Exists
- Is not a drive root
- Contains
cygwinin the path
- Takes ownership and resets permissions (as above).
- Deletes the directory recursively.
- Ensures the path:
-
Environment variable cleanup
- For both Machine and User scopes:
- Retrieves the
PATHvalue. - Splits it into entries and removes any that contain
cygwin(case-insensitive). - Writes back the cleaned
PATHvalue if changes were made. - Removes the
CYGWINenvironment variable if it exists.
- Retrieves the
- For both Machine and User scopes:
-
Registry key cleanup
Removes the following keys if present:
HKLM:\SOFTWARE\CygwinHKCU:\Software\CygwinHKLM:\SOFTWARE\WOW6432Node\CygwinHKCU:\Software\Cygnus SolutionsHKLM:\SOFTWARE\Cygnus SolutionsHKLM:\SOFTWARE\WOW6432Node\Cygnus Solutions
These cover both newer Cygwin installs and some older naming (
Cygnus Solutions). -
Shortcut and Start Menu cleanup
Searches common Desktop and Start Menu locations:
- Public desktop:
%PUBLIC%\Desktop - Current user desktop
- Start Menu (all users):
%ProgramData%\Microsoft\Windows\Start Menu\Programs - Start Menu (current user):
%APPDATA%\Microsoft\Windows\Start Menu\Programs
For each location:
- Deletes any shortcuts matching:
Cygwin*.lnkCygwin*Terminal*.lnk
- Removes any subfolders whose name starts with
Cygwin.
- Public desktop:
- Operating system: Windows (tested on Windows 10 / Windows 11)
- PowerShell: Windows PowerShell 5.x or PowerShell 7+
- Permissions: Must be run from an elevated (Administrator) PowerShell session
-
Download or clone the repository
git clone https://github.com/<your-username>/<your-repo-name>.git cd <your-repo-name>
-
Open PowerShell as Administrator
- Start Menu → search for “PowerShell”
- Right-click → “Run as administrator”
-
Run the script
If your execution policy blocks the script, you can temporarily bypass it:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass .\Uninstall-Cygwin.ps1
-
Reboot
After the script completes, it is recommended to reboot Windows to ensure:
- Any remaining
cygwin1.dllinstances are unloaded - Any open handles into previously deleted directories are released
- Any remaining
The script is designed to be idempotent:
- If Cygwin has already been removed:
- No services will match the Cygwin filter.
- No Cygwin directories will be found.
- PATH will already be cleaned.
- Registry keys and shortcuts will not exist.
In that state, running the script again results in harmless “nothing to do” behaviour.
The script includes several safeguards:
-
No deletion of drive roots
Any path where
Path == drive root(e.g.C:\) is explicitly skipped. -
Cygwin-only path deletion
Directories are only eligible for deletion if their path contains the word
cygwin(case-insensitive). -
Conservative cache deletion
Package cache directories are only removed if:
- They are discovered via Cygwin’s own registry entries, and
- Their paths contain
cygwin.
-
Scoped environment cleaning
Only PATH entries containing
cygwinare removed. Other entries are preserved.
-
It does not attempt to uninstall or modify:
- WSL (Windows Subsystem for Linux)
- MSYS / MSYS2
- Git for Windows
- Any non-Cygwin POSIX-like environment
-
It does not remove:
- Non-Cygwin directories that happen to contain other toolchains
- Any files outside those explicitly matching Cygwin-related registry, path, or shortcut patterns
You need to start PowerShell with elevated rights:
- Close the current PowerShell window.
- Start Menu → type
powershell. - Right-click → Run as administrator.
- Run the script again.
The script attempts to take ownership and reset permissions using takeown.exe and icacls.exe.
If these still fail:
- Check if you have locked files in those directories (for example, a third-party antivirus or backup agent).
- Temporarily disable security tools that may be keeping handles open on the Cygwin directory.
- Reboot and re-run the script as Administrator.
Environment variable changes apply to future shells:
- Close and reopen PowerShell / Command Prompt.
- For GUI applications, log off and back on (or reboot) to ensure all processes see the updated PATH.
You can inspect current PATH variables with:
[Environment]::GetEnvironmentVariable('PATH', 'Machine') [Environment]::GetEnvironmentVariable('PATH', 'User')You should not see any segments containing cygwin after the script has run.
You may wish to:
- Tighten or relax the matching rules for:
- PATH entries (e.g. only match
C:\cygwin64\bin) - Directory deletions (e.g. include additional known Cygwin install paths)
- PATH entries (e.g. only match
- Add logging to a file instead of console output
- Integrate with configuration management or automation tools
The script is structured into logical sections and helper functions to make such changes straightforward.
You are free to use, modify, and distribute this script.
A simple option is to license the repository under the MIT Licence.
For example:
MIT License Copyright (c) 2025 VibeCoder01 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: ...