-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-WindowsCleanup.ps1
48 lines (40 loc) · 1.95 KB
/
Set-WindowsCleanup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<#
.SYNOPSIS
This script sets up Windows Cleanup on Windows 10 or 11 to run with all options selected as a weekly scheduled task.
The scheduled task will run every Friday at 10AM.
.Notes
Version: 1.0
Authors: Nick Ellis
Purpose: Windows cleanup options tend to change with each version of Windows 10/11. This script can be run to capture all
available options and run with all options selected.
#>
$options = Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\'
foreach ($option in $options)
{
$path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$($option.Name.Split('\')[7])"
$flags = Get-ItemProperty -Path $path
if (-not $flags.StateFlags0010)
{
New-ItemProperty -Path $path -Name 'StateFlags0010' -PropertyType DWord -Value 2
}
elseif ($flags.StateFlags0010 -ne 2)
{
Set-ItemProperty -Path $path -Name 'StateFlags0010' -Value 2
}
}
$action = New-ScheduledTaskAction -Execute 'Cleanmgr.exe' -Argument '/sagerun:10 /verylowdisk'
$trigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 2 -DaysOfWeek Friday -At 10am
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -DontStopOnIdleEnd -Compatibility Win8 -StartWhenAvailable
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings
$scheduledTask = Get-ScheduledTask -TaskName 'Automated Disk Cleanup' -ErrorAction SilentlyContinue
if ($null -eq $scheduledTask)
{
Register-ScheduledTask -TaskName 'Automated Disk Cleanup' -InputObject $task
}
else
{
Unregister-ScheduledTask -TaskName 'Automated Disk Cleanup' -Confirm:$false
Register-ScheduledTask -TaskName 'Automated Disk Cleanup' -InputObject $task
}
Start-ScheduledTask 'Automated Disk Cleanup'