How to delete the "Windows.old" folder in Windows 8 (VBScript)

Introduction
This script shows how to delete the "Windows.old" folder in Windows 8.

Scenarios
Assume that you perform a refresh of Windows 8, an upgrade to Windows 8, a custom installation of Windows 8 without formatting the drive, or install Windows 8 on the same partition of a previous Windows installation. In this situation, you may have a "C:\Windows.old" folder left over after your new installation. This folder contains a copy of the previous Windows 8 installation, and can be very large. If users do not have to have this folder, they can use a script to delete the folder.

Script

Step 1: Double click the script and then it will show a dialog asking permission.

 

Step 2: Then there will be a dialog to remove the Windows.old file.

 

Here are some code snippets for your reference.

VBS
Edit|Remove
Dim objshell, Path
'Create wscript.shell object 
Set objshell = CreateObject("Wscript.shell")
'Run history registry path
Path = objshell.ExpandEnvironmentStrings("%HOMEDRIVE%") & "\Windows.old"


Set Fsobject = CreateObject("Scripting.filesystemobject")
If Fsobject.FolderExists(Path) Then 
     Regpath = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations\StateFlags1221"
     objshell.RegWrite Regpath, 2, "REG_DWORD"
     objshell.Run "cleanmgr /SAGERUN:1221", 1, True 
     WScript.Echo "Delete Windows.old folder successfully."
Else 
    
    WScript.Echo "Not find Windows.old folder"
End If 


End If 

Prerequisite

Windows 8

 

Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.