Use a Batch Script to Remotely Delete Windows.old Folder

Once an in-place upgrade of Windows is performed on a machine, a Windows.old folder will be created on the root of the OS drive.  This folder contains a backup of old data prior to the upgrade, including program files, Windows files, and AppData.  In the case of the update being reverted, Windows will resort back to the data in Windows.old.

This folder can be quite large, even taking up 20+ GB of space.  Windows does automatically delete the folder 30 days after the upgrade takes place, but because the folder is so large it can be beneficial to delete it before then.

If you attempt to delete Windows.old folder through regular methods, you will receive an error:

windowsolddeleteerror

Therefore there are other methods for deleting this folder.

Microsoft’s Supported Method

Microsoft’s supported method is to use Windows’ Disk Cleanup tool to delete “previous windows installations”, but they do not provide a straightforward way to do this remotely or on a large scale.

The Remote Method

I’ve come up with a couple of solutions for deleting Windows.old folders remotely, but the one that’s proven easiest is to use a batch script that contains three short CMDs:

TAKEOWN /F %SystemDrive%\Windows.old /A /R /D Y
ICACLS %SystemDrive%\Windows.old /T /grant :r Administrators:F
RD /S /Q %SystemDrive%\Windows.old

The first command takes ownership of the Windows.old folder and contents.  The next changes permissions.  The last removes the entire Windows.old directory.  Copy these commands into a file saved with a .BAT extension to create a working batch script.

In the case of my work environment, I created an SCCM application that simply runs this batch file which I can deploy on a large scale.  Group Policy can also be used to force run the script on startup, shutdown, logon, or logoff.

An Odd Problem I’ve Seen…

One problem I noticed when using this script is it didn’t always reliably remove the entire Windows.old contents.  Most files would be deleted, but some still didn’t have the appropriate permissions applied and were therefore not deleted.  This seemed to happen on roughly 10% of computers I deployed to.

After some testing, I found that re-running the same three commands over again does successfully and reliably clean up the remaining Windows.old content.  Therefore I modified my script to run the above set of commands twice in a row.

This script has allowed me to distribute Windows upgrades like the recent Windows 10 Anniversary Update and gain control of when to delete Windows.old folders.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: