11 January 2021

Automating the Deletion of Windows.old

It is the beginning of 2021 and my first project for the new year is upgrading all systems to Windows 10 20H2. At the end of the upgrades comes the cleanup and there is no clean way to do this for system admins. Cleanmgr.exe is now deprecated as of Windows 10 2004. There is not a PowerShell option for controlling storage sense. Cleanmgr.exe /AUTOCLEAN would open up cleanmgr.exe and then freeze. It never deleted the folder and was stuck at zero CPU usage. I also tried using PSEXEC to execute it with the same results. Another suggestion was using task scheduler. I also tried using Dism.exe /online /Cleanup-Image /StartComponentCleanup. The Windows.old folder was still present. The next option is to delete the folder. Here is a pic of the failure to clean up the Windows.old folder using a domain admin account and cmd.exe run as administrator.

NOTE: There is an issue we ran into when someone had a USB drive connected. The system connected to the wrong drive. and installed the SMSTaskSequence on that drive instead of the bootable C: drive. This caused the system to reboot constantly because it then did not see the Windows.old directory to delete. I fixed this by adding logic that looked for the Windows.old directory. The new code is below



NOTE: Once this folder has been deleted, Windows cannot be reverted back to the previous version. 

Once the upgrade has taken place, the Windows.old folder is present. It typically takes up 15+ GB of space. The upgraded OS is still actively using some files in the directory as I learned while exploring how to delete them. I found the files being used were drivers. This prevents you from deleting it while the OS is in memory. The alternative is to load WinPE so the OS is not in memory thereby freeing up the directory for deletion. Once the directory is deleted in WinPE and the system reboots, the OS reassociates the drivers it was using in the Windows.old directory to the Windows directory.

I first tried using PowerShell to delete the directory and there was a constant problem. PowerShell could not delete all files and directories, no matter what I tried. I consistently got the message "No mapping between account names and security IDs was done." Next, I tried RMDIR and it worked perfectly. I was able to use PowerShell to both find the drive in the WinPE environment and then execute the RMDIR command to delete the Windows.old directory on that drive. I tried this as a one-liner, but it was hit and miss with the RMDIR accepting the piped output instead of a variable. I found storing the path to the Windows.old directory in a variable was much more reliable. Here is the two-line code below:

$Drive = (Get-Partition | Where-Object {((Test-Path ($_.DriveLetter + ':\Windows.old')) -eq $True)}).DriveLetter
If ((Test-Path ($Drive + ':\Windows.old')) -eq $true) {
    $Directory = $Drive + ':\Windows.old'
    cmd.exe /c rmdir /S /Q $Directory
}

Here is a screenshot of the task sequence I used to deploy to the systems. The first restart is to load WinPE, the second sequence is the above PowerShell script, and the third sequence reboots the system back into the installed OS.



Related Posts:

  • Automate Unmounting WIM Files This script will allow you to painlessly mount WIM files for editing. It runs relative to its location it was executed from. Here is the link to download this script. '***************************************************… Read More
  • Stopping system from locking Most companies have a security policy enabled that locks a computer after a certain time interval to stop other users from being able to access the computer if the user forgets to lock the system when they step away. There a… Read More
  • Powershell: Adding powershell Run-as Administrator Option For some Powershell scripts, they need the elevated privileges, even when UAC is disabled. In order to do this, there are two options. Run a cmd.exe session as administrator and execute the PS1 file from the… Read More
  • APPLYING WINDOWS UPDATE MSU FILES TO WIM IMAGES This powershell script will inject all .msu files into a mounted WIM file. The script first scans the relative path of the directory that it was executed from for WIM files. Next, it mounts the WIM file. The script then sca… Read More
  • Setting Acrobat and Reader to autoupdate Here is a powershell script I wrote that adds the registry keys for both reader and acrobat to make them automatically update. This is intended for admins to deploy out to all users in a firm. It doesn't hurt to add both ke… Read More

4 comments:

  1. This work really well! My only change would be to expand out from a one-liner to something easier to read or troubleshoot, a `multi-liner` if you will.

    My old peers at Wells wouldn't let us check in anything with one-liners!

    ReplyDelete
    Replies
    1. I ended up putting it into two lines. The one-liner didn’t work out in this scenario.

      Delete
  2. I guess they're not the sharpest tools in the toolshed.

    ReplyDelete
  3. This did not worked for me. the computer rebooted to boot image, the script run, no error, the it restarted to OS, but the win.old folder is still there. Is there a log for this? the only one i saw it created was wpeinit.log and this log dont say nothing relevant.

    ReplyDelete