Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :: WARNING: This script deletes files to the recycle bin. You are responsible for reviewing the script,
- :: understanding its function, and ensuring that important data is backed up before execution.
- :: The author accepts no responsibility for data loss, system instability, or any other damages resulting from its use.
- ::
- :: WARNING: AI says i'm dumb for using this script, so probably not safe to use! (i use it though :D)
- :: AI: "Windows Installer is complicated.
- :: Microsoft does not guarantee that every file in C:\Windows\Installer is represented by a LocalPackage registry value.
- ::
- :: Examples include:
- :: superseded patches
- :: rollback data
- :: servicing information
- :: repair scenarios
- :: vendor-specific installer behavior
- ::
- :: Modern Windows servicing has become considerably more complex since this script was originally conceived.
- :: Because of that, Microsoft's guidance has long been:
- :: Don't manually clean the Windows Installer cache.
- :: That's also why Microsoft never released an official 'clean orphaned installers' tool."
- @echo off
- REM Version 14
- title Installer Cleaner
- color 05
- :CheckPermissions
- fltmc >nul 2>&1 || (
- echo Requesting administrative privileges . . .
- powershell -NoProfile -Command "$p=@{FilePath='%~f0';Verb='RunAs'};if('%*'){$p.ArgumentList='%*'};Start-Process @p" 2>nul
- exit /b 0
- )
- REM SOURCE: https://ourrick.wordpress.com/2015/04/04/find-orphaned-installer-files-in-cwindowsinstaller-folder/ <-- NOTE: There is some major bugs in that script!
- REM Where i first learned of the script: https://sourceforge.net/p/patchcleaner/discussion/general/thread/73e4f56a/?limit=25
- REM http://www.codeproject.com/Tips/119828/Running-a-bat-file-as-administrator-Correcting-cur
- setlocal enableextensions enabledelayedexpansion
- cd /d "%~dp0"
- if /i "%~1" == "nopause" (
- set "NOPAUSE=1"
- ) else (
- set "NOPAUSE=0"
- )
- if /i "%~2" == "purge" (
- set "PURGE=Y"
- ) else (
- set "PURGE=N"
- )
- set "LOG_DIRECTORY=%~dp0Logs\"
- set "FAILURE_TO_DELETE=0"
- set "MISSING_INSTALLER=0"
- :Choice
- if /i "%PURGE%" equ "Y" goto :Work
- :PROMPT_ONE
- cls
- set "PURGE=N"
- set /p PURGE=Delete orphaned installer files to Recycle Bin or just search for orphans? [y/N]:
- if /i "%PURGE%" equ "Y" goto :Work
- if /i "%PURGE%" equ "N" goto :Work
- goto :PROMPT_ONE
- :Work
- REM Create intermediate directories for log directory if necessary.
- if not exist "!LOG_DIRECTORY!" (
- mkdir "!LOG_DIRECTORY!" 2>nul
- if not exist "!LOG_DIRECTORY!" (
- echo Directory could not be created: "!LOG_DIRECTORY!"
- exit /b 1
- )
- )
- set TRACKED="!LOG_DIRECTORY!INSTALLER CLEANER - Tracked.log"
- if exist %TRACKED% del %TRACKED%
- set MISSING="!LOG_DIRECTORY!INSTALLER CLEANER - Missing.log"
- if exist %MISSING% del %MISSING%
- set ORPHANED="!LOG_DIRECTORY!INSTALLER CLEANER - Orphaned.log"
- if exist %ORPHANED% del %ORPHANED%
- set INSTALLERS="!LOG_DIRECTORY!INSTALLER CLEANER - Installers.log"
- if exist %INSTALLERS% del %INSTALLERS%
- echo Caching installer references...
- REM This will find both .msi and .msp entries.
- set "KEY_NAME=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData"
- set "VALUE_NAME=LocalPackage"
- for /f "usebackq tokens=1-2*" %%a in (`reg query "%KEY_NAME%" /s /v "%VALUE_NAME%" 2^>nul ^| findstr /i "%VALUE_NAME%"`) do (
- REM Append installer path to file.
- echo %%c>>%INSTALLERS%
- )
- if not exist %INSTALLERS% (
- echo.
- echo ERROR: No LocalPackage entries were found under:
- echo %KEY_NAME%
- echo This is almost certainly a failed registry read rather than a
- echo genuinely empty installer cache. Aborting without touching
- echo any files under %WINDIR%\Installer.
- echo.
- pause
- goto :eof
- )
- if /i "%PURGE%" equ "N" (
- echo Looking for orphaned installers...
- ) else (
- echo Looking for orphaned installers to delete...
- )
- for /f "delims=" %%a in ('dir /b /s %WINDIR%\Installer\*.msi %WINDIR%\Installer\*.msp') do (
- REM Added /I flag (Specifies that the search is not to be case-sensitive) to fix issue where M$ inconsistently lists installer paths in: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData
- REM The LocalPackage key can contain upper and lower-case variations, e.g.
- REM c:\Windows\Installer\177be02.msp
- REM C:\Windows\Installer\32dc90.msi
- REM Its bad because a file can be incorrectly classified as orphaned because findstr is case-sensitive by default.
- findstr /i /c:"%%a" %INSTALLERS% >nul
- if !errorlevel! equ 1 (
- echo "%%a" ORPHANED>>%ORPHANED%
- echo "%%a" ORPHANED
- if /i "%PURGE%" equ "Y" (
- REM del "%%a"
- REM
- REM Send orphaned installer files to the Recycle Bin instead of permanently deleting them.
- powershell.exe -NoProfile -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('%%a', [Microsoft.VisualBasic.FileIO.UIOption]::OnlyErrorDialogs, [Microsoft.VisualBasic.FileIO.RecycleOption]::SendToRecycleBin)"
- if exist "%%a" (
- echo FAILED to recycle - %%a
- set FAILURE_TO_DELETE=1
- ) else (
- echo Deleted orphaned installer - %%a
- )
- )
- ) else (
- echo "%%a" TRACKED>>%TRACKED%
- )
- )
- echo Looking for missing installers...
- for /f "delims=" %%a in ('type %INSTALLERS%') do (
- if not exist "%%a" (
- echo "%%a" MISSING>>%MISSING%
- echo "%%a" MISSING
- set MISSING_INSTALLER=1
- )
- )
- :End
- if %FAILURE_TO_DELETE% equ 1 pause
- if %MISSING_INSTALLER% equ 1 (
- echo -----
- echo Search this registry key with the installer path to determine its DisplayName:
- echo HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData
- echo Find an installer online with that name and download it.
- echo Your looking for installers that offer a Repair option the first time you run them.
- echo Click Repair, click Browse to find the vc_red.msi file, and click Ok to reinstall.
- echo -----
- pause
- )
- if %NOPAUSE% equ 0 (
- pause
- ) else (
- timeout /t 7 /nobreak
- )
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment