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

Introduction

This PowerShell 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

Step1: Right click the script and select with run with administrator.  

Then it will start Disk Cleanup program. After the program finishes, Windows.old folder will be removed.

Here are some code snippets for your references.

PowerShell
Edit|Remove
$path = $env:HOMEDRIVE+"\windows.old" 
If(Test-Path -Path $path) 
{ 
    #create registry value 
    $regpath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations" 
    New-ItemProperty -Path $regpath -Name "StateFlags1221" -PropertyType DWORD  -Value 2 -Force  | Out-Null 
    #start clean application 
    cleanmgr /SAGERUN:1221} 
Else 
{ 
    Write-Warning "There is no 'Windows.old' folder in system driver" 
    cmd /c pause  
}

Prerequisite

Windows 8 or later version

Additional Resources

Technical Resource: 
Test-Path

http://technet.microsoft.com/en-us/library/hh849776.aspx