Properly remove windows.old on Hyper-V Server 2012 R2

Solution 1:

I first tried to copy and run cleanmgr.exe (Disk Cleanup tool), but it has too many dependencies on DLLs which are not present in Core/Hyper-V Server.

So instead I deleted the directory manually.

First I removed all junction points and symbolic links. To do this I used junction.exe from SysInternals. Copy the exe into a directory in your path. I ran it to get a list of all junctions:

c:\tools\junction.exe -s -q C:\windows.old > %temp%\junc.txt

I opened a PowerShell:

start powershell.exe

and ran the following script to find the relevant lines and execute junction.exe again:

 foreach ($line in [System.IO.File]::ReadLines("$env:temp\junc.txt"))
 {
     if ($line -match "^\\\\")
     {
         $file = $line -replace "(: JUNCTION)|(: SYMBOLIC LINK)",""
         & c:\tools\junction.exe -d "$file"
     }
 }

This removed all junction points and the single symbolic link on my system.

back in cmd.exe I now executed three commands to clear permissions and delete all files:

 takeown /F C:\windows.old /R /D Y
 cacls C:\windows.old /T /G Everyone:F
 rd /s /q C:\windows.old

In my test, I installed a new Hyper-V server 2012, then upgraded to 2012 R2, Windows.old is now gone and the system is running fine with all old junction targets intact.

Solution 2:

I used Peter H's answer and confirmed it does work, however I needed to do this on multiple servers, so I modified his code into a powershell script that can be executed either locally or via PS remoting from another machine. This is the .ps1 file:

# Script to remove windows.old after an upgrade

# Assumes path to sysinternals is in the PATH env variable


$ErrorActionPreference = "Inquire"

junction.exe -accepteula -s -q C:\windows.old | out-file $env:temp\juncts.txt -force

foreach ($line in [System.IO.File]::ReadLines("$env:temp\juncts.txt"))
 {
     if ($line -match "^\\\\")
     {
         $file = $line -replace "(: JUNCTION)|(: SYMBOLIC LINK)",""
         & junction.exe -d "$file"
     }
 }

 takeown /F C:\windows.old /R /D Y
 echo y | cacls C:\windows.old /T /G Everyone:F

 rm C:\windows.old -recurse -force
 rm "$env:temp\juncts.txt" -force