Windows.old Purge Script

This is a useful script to periodically remove the bloated windows.old directory which is becoming more and more common in the age of windows 10 and server 2019.  Should you ever come to rely on contents of the windows.old folder, it’s likely that your looking at a full rebuild.  If you have tried removing the folder manually, in most cases you will find that it 1.) takes forever 2.) not all of it can be removed – hence the script.  Can be deployed via task scheduler or group policy to run on shutdown…or as always, manual execution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@echo off
setlocal
Title Windows.Old Purge Script
set BAT_HOME=%~dp0
set BAT_NAME=%~s0
set BATFILE=%~nx0
set DRVLET=%~d0
if "%1"=="" goto interactive
REM Flash related lines are meant for deleting Flash specific .OCX files which cannot be easily deleted
if exist %1\Windows\System32\Macromed\Flash CD /D %1\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F
cd /d %DRVLET%
takeown /f %1 /R /d y
icacls %1 /grant administrators:F /t
rd /s /q %1
goto end
:interactive
cls
cd /d %DRVLET%\
IF EXIST %DRVLET%\Windows.old ECHO. && ECHO Found %DRVLET%\Windows.old && GOTO Auto
REM IF NOT EXIST %DRVLET%\Windows.old ECHO. && ECHO NOT FOUND %DRVLET%\Windows.old && ECHO.
GOTO end
REM echo Enter folder name [with complete path] which needs to be deleted:
REM echo.
REM echo To exit type "x" and press Enter key
REM echo.
REM echo.
REM set /p user=Folder Name[with path]:
REM if "%user%"=="x" goto end
REM if exist %user%\Windows\System32\Macromed\Flash CD /D %user%\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F
REM cd /d %DRVLET%\
REM takeown /f %user% /R /d y
REM icacls %user% /grant administrators:F /t
REM rd /s /q %user%
REM if not "%user%"=="x" goto interactive
REM goto end
:Auto
cls
ECHO.
cd /d %DRVLET%\
set user=Windows.old
if exist %DRVLET%\%user%\Windows\System32\Macromed\Flash CD /D %DRVLET%\%user%\Windows\System32\Macromed\Flash && cacls *.* /P Everyone:F
cd /d %DRVLET%\
takeown /f %user% /R /d y
icacls %user% /grant administrators:F /t
rd /s /q %user%
goto end
:end
Lima

About the author

Lima is the visual nautical indicator for "stop instantly."

Comments

Leave a Reply