forked from adbertram/Random-PowerShell-Work
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
542 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,116 @@ | ||
function Invoke-WindowsDiskCleanup { | ||
param() | ||
#requires -Version 5 | ||
|
||
Write-Log -Message 'Clearing CleanMgr.exe automation settings.' | ||
<#PSScriptInfo | ||
$getItemParams = @{ | ||
Path = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' | ||
Name = 'StateFlags0001' | ||
ErrorAction = 'SilentlyContinue' | ||
.VERSION 1.1 | ||
.GUID 0ef579e1-d89d-4e8a-9b9a-f07ab5af1084 | ||
.AUTHOR Adam Bertram | ||
.COMPANYNAME Adam the Automator, LLC | ||
.COPYRIGHT | ||
.TAGS | ||
.LICENSEURI | ||
.PROJECTURI | ||
.ICONURI | ||
.EXTERNALMODULEDEPENDENCIES | ||
.REQUIREDSCRIPTS | ||
.EXTERNALSCRIPTDEPENDENCIES | ||
.RELEASENOTES | ||
.PRIVATEDATA | ||
#> | ||
|
||
<# | ||
.DESCRIPTION | ||
A PowerShell wrapper script to automate the Windows Disk Cleanup utility. | ||
#> | ||
|
||
param( | ||
[Parameter()] | ||
[ValidateNotNullOrEmpty()] | ||
[string[]]$Section | ||
) | ||
|
||
$sections = @( | ||
'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' | ||
) | ||
|
||
if ($PSBoundParameters.ContainsKey('Section')) { | ||
if ($Section -notin $sections) { | ||
throw "The section [$($Section)] is not available. Available options are: [$($sections -join ',')]." | ||
} | ||
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-Log -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 | ||
} else { | ||
$Section = $sections | ||
} | ||
|
||
Write-Verbose -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 | ||
|
||
Write-Verbose -Message 'Adding enabled disk cleanup sections...' | ||
foreach ($keyName in $Section) { | ||
$newItemParams = @{ | ||
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$keyName" | ||
Name = 'StateFlags0001' | ||
Value = 1 | ||
PropertyType = 'DWord' | ||
ErrorAction = 'SilentlyContinue' | ||
} | ||
$null = New-ItemProperty @newItemParams | ||
} | ||
|
||
Write-Log -Message 'Starting CleanMgr.exe...' | ||
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -Wait | ||
Write-Verbose -Message 'Starting CleanMgr.exe...' | ||
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -Wait | ||
|
||
Write-Log -Message 'Waiting for CleanMgr and DismHost processes...' | ||
Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process | ||
} | ||
Write-Verbose -Message 'Waiting for CleanMgr and DismHost processes...' | ||
Get-Process -Name cleanmgr, dismhost -ErrorAction SilentlyContinue | Wait-Process |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
<#PSScriptInfo | ||
.VERSION 1.0 | ||
.GUID 0ef579e1-d89d-4e8a-9b9a-f07ab5af1084 | ||
.AUTHOR Adam Bertram | ||
.COMPANYNAME Adam the Automator, LLC | ||
.COPYRIGHT | ||
.TAGS | ||
.LICENSEURI | ||
.PROJECTURI | ||
.ICONURI | ||
.EXTERNALMODULEDEPENDENCIES | ||
.REQUIREDSCRIPTS | ||
.EXTERNALSCRIPTDEPENDENCIES | ||
.RELEASENOTES | ||
.PRIVATEDATA | ||
#> | ||
|
||
<# | ||
.DESCRIPTION | ||
A PowerShell wrapper script to automate the Windows Disk Cleanup utility. | ||
#> | ||
Param() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
<#PSScriptInfo | ||
.VERSION 1.0 | ||
.GUID b918ed36-48fa-4577-b2bf-c4d9225cd095 | ||
.AUTHOR Adam Bertram | ||
.COMPANYNAME Adam the Automator, LLC | ||
.COPYRIGHT | ||
.TAGS | ||
.LICENSEURI | ||
.PROJECTURI | ||
.ICONURI | ||
.EXTERNALMODULEDEPENDENCIES | ||
.REQUIREDSCRIPTS | ||
.EXTERNALSCRIPTDEPENDENCIES | ||
.RELEASENOTES | ||
.PRIVATEDATA | ||
#> | ||
|
||
<# | ||
.DESCRIPTION | ||
A simple script to determine the true uptime of one or more servers. | ||
#> | ||
Param() | ||
|
||
|
Oops, something went wrong.