:: Released under the GNU General Public License version 3 by J2897.
@echo OFF
setlocal
title Cygwin Remover
cls
:: Cygwin's POSIX permissions often confuse people whom then try to adjust
:: permissions from Windows - which can be brutal for delicate, little Cygwin.
:: Cygwin's permissions are only meant to be configured from within Cygwin. So
:: if you've accidentally messed up Cygwin's permissions, this may help you to
:: remove Cygwin ready for a fresh, warm and cosy, reinstall.
:: Inspired by...
:: http://superuser.com/questions/167219/how-to-completely-uninstall-remove-cygwin-from-windows-7
set "TAB= "
set "CYGUSER=cyg_server"
set "ERRORS=0"
ver | find "Version 6." >nul
if %ERRORLEVEL% EQU 0 (
set "OS_VERSION=Windows 7"
goto :Checks
)
ver | find "Version 5." >nul
if %ERRORLEVEL% EQU 0 (
set "OS_VERSION=Windows XP"
)
:Checks
if not defined OS_VERSION (
echo Unsupported operating system.
echo.
goto :End
)
if "%OS_VERSION%"=="Windows 7" (
REM Do OPENFILES to check for administrative privileges
openfiles >nul
if errorlevel 1 (
color cf
echo Right-click on this file and select 'Run as administrator'.
pause
color
exit /b 1
)
)
if exist "%SYSTEMDRIVE%\cygwin" (set "MCF=%SYSTEMDRIVE%\cygwin")
if exist "%SYSTEMDRIVE%\cygwin64" (set "MCF=%SYSTEMDRIVE%\cygwin64")
if not defined MCF (
echo Cygwin folder doesn't exist.
goto :end
)
net user | find "%CYGUSER%" >nul
if %ERRORLEVEL% NEQ 0 (
echo Unable to proceed. The user %CYGUSER% doesn't exist.
goto :end
)
if exist "%MCF%\home\%USERNAME%\.bashrc" (
echo WTF^? Dude, I ain't destroying your ".bashrc" file:
echo %MCF%\home\%USERNAME%\.bashrc
explorer /select,"%MCF%\home\%USERNAME%\.bashrc"
goto :end
)
set "DELSERV=0"
echo Stopping and deleting services created by the user "%CYGUSER%"...
for /F %%A in ('wmic service get name^, StartName ^|find ".\%CYGUSER%"') do (
net stop "%%A" && sc delete "%%A"
if %ERRORLEVEL% NEQ 0 (set "DELSERV=1")
)
if %DELSERV% EQU 1 (set /a "ERRORS+=1" & echo There was a problem.)
echo Taking ownership of the Main Cygwin Folder...
takeown /F "%MCF%" /R /D Y >nul
if %ERRORLEVEL% NEQ 0 (
set /a "ERRORS+=1"
echo There was a problem.
) else (
echo Done.
)
echo Resetting permissions...
if "%OS_VERSION%"=="Windows XP" (
icacls "%MCF%" /reset /T /C /Q >nul 2>&1
) else (
icacls "%MCF%" /reset /T /C /L /Q
)
if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
echo Removing the Main Cygwin Folder...
RD /S /Q "%MCF%"
if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
echo Deleting users...
net user | find "%CYGUSER%" >nul
if %ERRORLEVEL% EQU 0 (
net user %CYGUSER% /delete
)
net user | find "sshd" >nul
if %ERRORLEVEL% EQU 0 (
net user sshd /delete
)
if "%OS_VERSION%"=="Windows XP" (
set "CUF=%SYSTEMDRIVE%\Documents and Settings\%CYGUSER%"
) else (
set "CUF=%SYSTEMDRIVE%\Users\%CYGUSER%"
)
echo Taking ownership of the Cygwin User Folder...
takeown /F "%CUF%" /R /D Y >nul
if %ERRORLEVEL% NEQ 0 (
set /a "ERRORS+=1"
echo There was a problem.
) else (
echo Done.
)
:: I think the errorlevel of "if" will be returned instead of "icacls". 'Needs testing.
echo Resetting permissions...
if "%OS_VERSION%"=="Windows XP" (
icacls "%CUF%" /reset /T /C /Q >nul 2>&1
) else (
icacls "%CUF%" /reset /T /C /L /Q
)
if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
echo Removing the Cygwin User Folder...
RD /S /Q "%CUF%"
if %ERRORLEVEL% NEQ 0 (set /a "ERRORS+=1" & echo There was a problem.)
echo Deleting firewall entry...
if exist "%SYSTEMDRIVE%\Program Files (x86)" (
netsh advfirewall firewall delete rule name="OpenSSH"
) else (
netsh firewall delete allowedprogram "%SYSTEMDRIVE%\cygwin\usr\sbin\sshd.exe"
)
echo Cleaning registry...
reg delete HKCU\Software\Cygwin /f >nul 2>&1
reg delete HKLM\Software\Cygwin /f >nul 2>&1
echo.
if %ERRORS% EQU 0 (
echo Cygwin was removed absolutely perfectly^! ^:^)
) else (
echo The removal of Cygwin didn't go so well. There were at least %ERRORS% problem^(s^).
)
if exist "%USERPROFILE%\.bashrc" (
echo.
echo Don't forget about your ".bashrc" file:
echo %USERPROFILE%\.bashrc
)
:end
endlocal
echo.
pause