Skip to content

Commit

Permalink
Added Windows Disk Cleanup script
Browse files Browse the repository at this point in the history
  • Loading branch information
adbertram committed Apr 28, 2017
1 parent 75003c1 commit eed1cb7
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 93 deletions.
87 changes: 0 additions & 87 deletions PowerShell Internals/Wait-Action-Help.xml

This file was deleted.

41 changes: 35 additions & 6 deletions PowerShell Internals/Wait-Action.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
# .ExternalHelp C:\Dropbox\GitRepos\Random-PowerShell-Work\PowerShell Internals\Wait-Action-Help.xml
<#PSScriptInfo
.VERSION 1.0
.GUID 389989f2-626a-45cc-aa5c-2df2f93cee03
.AUTHOR Adam Bertram
.COMPANYNAME Adam the Automator, LLC
.PROJECTURI https://github.com/adbertram/Random-PowerShell-Work/blob/master/PowerShell%20Internals/Wait-Action.ps1
#>

<#
.SYNOPSIS
A script to wait for an action to finish.
.DESCRIPTION
This script executes a scriptblock represented by the Condition parameter continually while the result returns
anything other than $false or $null.
.PARAMETER Condition
A mandatory scriptblock parameter representing the code to execute to check the action condition. This code
will be continually executed until it returns $false or $null.
.PARAMETER Timeout
A mandatory integer represneting the time (in seconds) to wait for the condition to complete.
.PARAMETER ArgumentList
An optional collection of one or more objects to pass to the scriptblock at run time. To use this parameter,
be sure you have a param() block in the Condition scriptblock to accept these parameters.
.PARAMETER RetryInterval
An optional integer representing the time (in seconds) between the code execution in Condition.
.EXAMPLE
PS> Wait-Action -Condition { (Get-Job).State | where { $_ -ne 'Running' } -Timeout 10
This example will wait for all background jobs to complete for up to 10 seconds.
#>

[OutputType([void])]
[CmdletBinding()]
Expand All @@ -20,7 +54,6 @@ param
[ValidateNotNullOrEmpty()]
[int]$RetryInterval = 5
)
$ErrorActionPreference = 'Stop'
try
{
$timer = [Diagnostics.Stopwatch]::StartNew()
Expand All @@ -39,8 +72,4 @@ try
catch
{
Write-Error -Message $_.Exception.Message
}
finally
{
$ErrorActionPreference = 'Continue'
}
76 changes: 76 additions & 0 deletions Random Stuff/Invoke-WindowsDiskCleanup.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<#
.SYNOPSIS
This script invokes the Windows Disk Cleanup utility, enables all rules and runs it.
.EXAMPLE
PS> .\Invoke-WindowsDiskCleanup.ps1
#>

Write-Log -Message 'Clearing CleanMgr.exe automation settings.'

$getItemParams = @{
Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*'
Name = 'StateFlags0001'
ErrorAction = 'SilentlyContinue'
}
Get-ItemProperty @getItemParams | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue

$enabledSections = @(
'Active Setup Temp Folders'
'BranchCache'
'Content Indexer Cleaner'
'Device Driver Packages'
'Downloaded Program Files'
'GameNewsFiles'
'GameStatisticsFiles'
'GameUpdateFiles'
'Internet Cache Files'
'Memory Dump Files'
'Offline Pages Files'
'Old ChkDsk Files'
'Previous Installations'
'Recycle Bin'
'Service Pack Cleanup'
'Setup Log Files'
'System error memory dump files'
'System error minidump files'
'Temporary Files'
'Temporary Setup Files'
'Temporary Sync Files'
'Thumbnail Cache'
'Update Cleanup'
'Upgrade Discarded Files'
'User file versions'
'Windows Defender'
'Windows Error Reporting Archive Files'
'Windows Error Reporting Queue Files'
'Windows Error Reporting System Archive Files'
'Windows Error Reporting System Queue Files'
'Windows ESD installation files'
'Windows Upgrade Log Files'
)

Write-Verbose -Message 'Adding enabled disk cleanup sections...'
foreach ($keyName in $enabledSections) {
$newItemParams = @{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$keyName"
Name = 'StateFlags0001'
Value = 1
PropertyType = 'DWord'
ErrorAction = 'SilentlyContinue'
}
$null = New-ItemProperty @newItemParams
}

Write-Verbose -Message 'Starting CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -Wait

Write-Verbose -Message 'Waiting for CleanMgr and DismHost processes...'
Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process

# if (Test-Path $env:SystemRoot\Logs\CBS\DeepClean.log) {
# if (Select-String -Path $env:SystemRoot\Logs\CBS\DeepClean.log -Pattern 'Total size of superseded packages:' -Quiet) {

# }
# }

0 comments on commit eed1cb7

Please sign in to comment.