Delete' Windows.old' folder using powershell

  • Delete' Windows.old' folder using powershell

    Posted by Roddy Gelberty on 21 July 2020 at 4:05 pm

    How can I delete the ‘Windows.old’ folder using a PowerShell script. I want to be able to do this after upgrading Windows to the latest version of Windows 10.

    I understand that Windows automatically deletes this folder but I want to be able to clear this space immediately so that other software distribution activities can continue.

    Luna Stella replied 2 years, 6 months ago 3 Members · 2 Replies
  • 2 Replies
  • Topaz George

    Organizer
    21 July 2020 at 8:21 pm

    Hope this helps.

    If(Test-Path -Path($((Get-WmiObject Win32_OperatingSystem).SystemDrive) +"\windows.old"))

    {

    New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations" -Name "StateFlags1221" -PropertyType DWORD -Value 2 -Force | Out-Null

    cleanmgr /SAGERUN:1221

    }

  • Luna Stella

    Member
    21 July 2020 at 9:02 pm

    A few amendments to Topaz’s answer. use ‘start-process‘ cmdlet to start ‘cleanmgr.exe‘.

    If(Test-Path -Path($((Get-WmiObject Win32_OperatingSystem).SystemDrive) +"\windows.old"))
    {
    New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations" -Name "StateFlags1221" -PropertyType DWORD -Value 2 -Force | Out-Null
    Start-Process -FilePath "cleanmgr" -ArgumentList /SAGERUN:1221 -Wait -WindowStyle Hidden
    }