hyper-v-server-2012 windows-server-core – Properly Remove windows.old on Hyper-V Server 2012 R2

hyper-v-server-2012windows-server-core

I have installed Hyper-V Server 2012 R2 on a server that had Hyper-V Server 2012. When I did this, the standard Windows.old folder was created. I now would like to remove that folder safely. The standard way to do that with a full GUI would be to use Disk Cleanup, but of course I don't have that option on Hyper-V Server.

Is there a formal way to remove that folder in this scenario? I know if this was Server Core I could install the full GUI including Desktop Experience, but that would be a lot of nonsense just to cleanly remove a folder.

My primary reason for asking, as opposed to just doing rmdir /s or some such, is that the Windows.old folder has a lot of junctions, and I don't want to break anything in the production OS copy as part of doing this.

Best Answer

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.