tboy1337 / Install-Uninstall-GIT-Windows

Simple batch scripts to automate the installation and uninstallation of Git on Windows systems.
https://github.com/tboy1337
MIT License
1 stars 1 forks source link

Uninstaller Known Issues #3

Open tboy1337 opened 1 week ago

tboy1337 commented 1 week ago
tboy1337 commented 1 week ago

Possibly fixed by the below code, this way you wouldn't be able to run the uninstaller from another batch file or would require user interaction on the target machine.

@echo off
setlocal enabledelayedexpansion

set "userLocalAppData=%LOCALAPPDATA%"
set "locations[0]=%ProgramFiles%\Git"
set "locations[1]=%SystemDrive%\Program Files (x64)\Git"
set "locations[2]=%ProgramFiles(x86)%\Git"
set "locations[3]=%SystemDrive%\Git"
set "locations[4]=%userLocalAppData%\Programs\Git"
set "found_installations=0"

    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)

if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params= %*
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"

echo Checking for Git installations...

for /L %%i in (0,1,4) do (
    if exist "!locations[%%i]!\unins*.exe" (
        set "uninstaller=!locations[%%i]!\unins*.exe"
        for %%f in ("!uninstaller!") do (
            set "found_uninstaller=%%f"
            set /a found_installations+=1
        )
    )
)

if %found_installations% equ 0 (
    echo No Git installations found in standard locations.
    echo This might be because Git is installed in a non-standard location.
    echo If you think Git is still installed, try to uninstall it through Control Panel or locate the installation folder and run the uninstaller manually.
    timeout /t 5 /nobreak
    exit /b 7
)

for /L %%i in (0,1,4) do (
    if exist "!locations[%%i]!\unins*.exe" (
        echo Found Git installation in !locations[%%i]! with uninstaller !found_uninstaller!

        echo Terminating running Git processes...
        taskkill /F /IM "bash.exe" 2>nul
        taskkill /F /IM "putty.exe" 2>nul
        taskkill /F /IM "puttytel.exe" 2>nul
        taskkill /F /IM "puttygen.exe" 2>nul
        taskkill /F /IM "pageant.exe" 2>nul

        echo Uninstalling Git from !locations[%%i]!...
        start /wait "Uninstalling Git" "!found_uninstaller!" /SP- /VERYSILENT /SUPPRESSMSGBOXES /FORCECLOSEAPPLICATIONS

        if !errorlevel! neq 0 (
            echo Failed to uninstall Git from !locations[%%i]!
            set "uninstall_error=1"
        ) else (
            echo Successfully uninstalled Git from !locations[%%i]!

            echo Cleaning up remaining files in !locations[%%i]!...
            timeout /t 2 /nobreak >nul

            rd /s /q "!locations[%%i]!" 2>nul
            if !errorlevel! neq 0 (
                echo Warning: Could not remove remaining files in !locations[%%i]!
                set "cleanup_error=1"
            ) else (
                echo Successfully removed remaining files in !locations[%%i]!
            )
        )
    )
)

if defined uninstall_error (
    echo One or more Git uninstallations failed.
    timeout /t 5 /nobreak
    exit /b 5
) else if defined cleanup_error (
    echo Git was uninstalled but some cleanup operations failed.
    echo Please check the listed locations and remove remaining files manually.
    timeout /t 5 /nobreak
    exit /b 6
) else (
    echo All Git installations were successfully uninstalled and cleaned up.
    timeout /t 5 /nobreak
    exit /b 0
)