/build/static/layout/Breadcrumb_cap_w.png

Removal of Windows.old after upgrade.

I am currently working on a script to run cleanmgr.exe to remove C:\Windows.old without any user interaction. Everything works except I am still getting a prompt to confirm that I want to delete the folder and I won't be able to restore. I need to have this fully automated. Any idea's would be greatly appreciated.

<#
.SYNOPSIS
Automates the Disk Clean tool to clean up all possible items for the given OS.
#>


# V2 admin check
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "Please run this script as an Administrator!"
    Exit 1
}

# Create reg keys
$volumeCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
foreach($key in $volumeCaches)
{
    New-ItemProperty -Path "$($key.PSPath)" -Name StateFlags0099 -Value 2 -Type DWORD -Force | Out-Null
}

# Run Disk Cleanup 
Start-Process -Wait "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList "/sagerun:99"

# Delete the keys
$volumeCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
foreach($key in $volumeCaches)
{
    Remove-ItemProperty -Path "$($key.PSPath)" -Name StateFlags0099 -Force | Out-Null
}

Remove-Item c:\windows.old\* -Force -recurse


0 Comments   [ + ] Show comments

Answers (1)

Posted by: wynnem 7 years ago
Orange Belt
0
Whats the exact prompt you get?



Comments:
  • "If you clean up previous installations or temporary installation files, you will no longer be able to restore the machine back to the previous version of Windows. Are you sure you want to do this?" - kolbenschlag 7 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ