<#----------------------------------------------------------------------------
LEGAL DISCLAIMER
This sample code is provided for the purpose of illustration only and is not
intended to be used in a production environment. THIS sample code AND ANY
RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. We grant You a
nonexclusive, royalty-free right to use and modify the sample code and to
reproduce and distribute the object code form of the sample code, provided
that You agree: (i) to not use Our name, logo, or trademarks to market Your
software product in which the sample code is embedded; (ii) to include a valid
copyright notice on Your software product in which the sample code is embedded;
and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and
against any claims or lawsuits, including attorneys’ fees, that arise or result
from the use or distribution of the sample code.
This script is provided "AS IS" with no warranties, and confers no rights. Use
of included script samples are subject to the terms specified
at http://www.microsoft.com/info/cpyright.htm.
----------------------------------------------------------------------------#>
$ErrorActionPreference = 'SilentlyContinue'
$OSVersion = (Get-WmiObject Win32_OperatingSystem).Version
If ($OSVersion -eq "6.1.7601")
{
If((Get-Hotfix KB2852386).HotFixID -ne "KB2852386")
{
Write-Host "KB2852386 is required for script. Please install hotfix and re-run." -ForegroundColor Red
Write-EventLog -LogName Application -Source "CleanMgrScript" -EntryType Warning -EventId 9090 -Message "OS Version is $OSVersion - KB2852386 not found, exiting without running CleanMgr."
Exit
}
}
$Windir = "$env:windir\System32"
$StateFlagClean = "2"
$SageSet = "0010"
$StateFlags = "StateFlags$SageSet"
$Cleanmgr = "$Windir\Cleanmgr.exe"
$args = "/sagerun:$SageSet"
$FreeSpaceBefore = (Get-WmiObject win32_logicaldisk | where { $_.DeviceID -eq $env:SystemDrive }).FreeSpace
$FreeSpaceBefore = "$([Math]::Round(($FreeSpaceBefore / 1GB),2))GB"
Write-Host "$FreeSpaceBefore free on $env:SystemDrive before running CleanMgr." -ForegroundColor Cyan
New-EventLog -LogName Application -Source "CleanMgrScript"
Write-EventLog -LogName Application -Source "CleanMgrScript" -EntryType Information -EventId 9091 -Message "OS Version is $OSVersion - CleanMgr script executing, free space on $env:SystemDrive before execution is $FreeSpaceBefore."
#Set all VolumeCache keys to StateFlags = 2 to allow cleanup.
$SubKeys = Get-Childitem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches
Foreach ($Key in $SubKeys)
{
Set-ItemProperty -Path $Key.PSPath -Name $StateFlags -Value $StateFlagClean
}
$process = New-Object system.Diagnostics.Process
$si = New-Object System.Diagnostics.ProcessStartInfo
$si.FileName = $cleanmgr
$si.Arguments = $args
$process.StartInfo = $si
$process.Start() | Out-Null
$processId = $process.Id
$process.WaitForExit()
while (Get-WmiObject win32_process -Filter "ParentProcessId = $processid")
{
sleep -seconds 1
}
$FreeSpaceAfter = (Get-WmiObject win32_logicaldisk | where { $_.DeviceID -eq $env:SystemDrive }).FreeSpace
$FreeSpaceAfter = "$([Math]::Round(($FreeSpaceAfter / 1GB),2))GB"
Write-Host "$FreeSpaceAfter free on $env:SystemDrive after running CleanMgr." -ForegroundColor Cyan
Write-EventLog -LogName Application -Source "CleanMgrScript" -EntryType Information -EventId 9092 -Message "OS Version is $OSVersion - CleanMgr script executed, free space on $env:SystemDrive after execution is $FreeSpaceAfter."