Enable and Configure Storage sense - Windows 10

Microsoft announced that it will begin depreciating the Disk Cleanup tool starting in Windows 10 v1809, and in its place they are ramping up the built-in Storage sense feature originally released with Windows 10.
Storage sense will automatically run cleanups when your disk is low on space, but you can configure it further to run on a schedule.
If you have a manageable version of Windows 10 (Pro, Ent, Edu), than you can configure Storage sense through GPO. If your devices are not domain joined (or you prefer not to use GPO to manage storage sense), this Worklet can help enable the feature, and set some of the most common configurations.

What are we doing in the settings?
Evaluation - The example here is fairly simple. It checks if the primary key exists. The key is only created when GPO settings are applied, or if you manually place them. The idea is, that if this key is already there, it will not run remediation which could change what was applied by GPO.

Remediation - Uses group policy registry keys to Enable and configure Storage Sense. An overview of all available settings are listed below (to date), and this script will Enable and force the following settings:
Description format includes - Setting Name, Description, Setting applied, and Setting description.

  1. Allow Storage Sense
    Description: Storage Sense can automatically clean some of the user’s files to free up disk space. By default, Storage Sense is automatically turned on when the machine runs into low disk space and is set to run whenever the machine runs into storage pressure. This cadence can be changed in Storage settings or set with the “Configure Storage Sense cadence” group policy.
    Enabled:Storage Sense is turned on for the machine, with the default cadence as ‘during low free disk space’. Users cannot disable Storage Sense, but they can adjust the cadence (unless you also configure the “Configure Storage Sense cadence” group policy)

  2. Allow Storage Sense Temporary Files cleanup
    Description: When Storage Sense runs, it can delete the user’s temporary files that are not in use.
    Not Configured: By default, Storage Sense will delete the user’s temporary files. Users can configure this setting in Storage settings.

  1. Configure Storage Sense Cloud Content dehydration threshold
    Description: When Storage Sense runs, it can dehydrate cloud-backed content that hasn’t been opened in a certain amount of days.
    Enabled - 30: You must provide the number of days since a cloud-backed file has been opened before Storage Sense will dehydrate it. Support values are: 0 - 365. If you set this value to zero, Storage Sense will not dehydrate any cloud-backed content. The default value is 0, or never dehydrating cloud-backed content.

  2. Configure Storage Storage Downloads cleanup threshold (yes, “Storage” is there two times as defined by Microsoft GPO)
    Description: When Storage Sense runs, it can delete files in the user’s Downloads folder if they have been there for over a certain amount of days.
    Enabled - 120: You must provide the minimum age threshold (in days) of a file in the Downloads folder before Storage Sense will delete it. Support values are: 0 - 365. If you set this value to zero, Storage Sense will not delete files in the user’s Downloads folder. The default is 0, or never deleting files in the Downloads folder.

  3. Configure Storage Sense cadence
    Description: Storage Sense can automatically clean some of the user’s files to free up disk space.
    Enabled - Every Month: You must provide the desired Storage Sense cadence. Supported options are: daily, weekly, monthly, and during low free disk space. The default is 0 (during low free disk space).

  4. Configure Storage Sense Recycle Bit cleanup threshold
    Description: When Storage Sense runs, it can delete files in the user’s Recycle Bin if they have been there for over a certain amount of days.
    Not Configured: By default, Storage Sense will delete files in the user’s Recycle Bin that have been there for over 30 days. Users can configure this setting in Storage settings.

TLDR: Enable Storage Sense, and configure to auto run monthly. Includes settings to manage cloud-backed files to reduce disk space usage.

NOTE: To reset the OS to default for Storage Sense, remove the following registry key: ‘HKLM:\Software\Policies\Microsoft\Windows\StorageSense’

Evaluation Code:

if(Test-Path 'HKLM:\Software\Policies\Microsoft\Windows\StorageSense'){
	return 0
 } else { return 1 }
 Exit $exitCode

Remediation Code:

<#
.SYNOPSIS
Enable Storage Sense and set standard configurations.

.DESCRIPTION
  Uses group policy registry keys to Enable and configure Storage Sense.  An overview of all available settings are listed below (to date), and this script will Enable and force the following settings: 
  
Allow Storage Sense 
Description: Storage Sense can automatically clean some of the user’s files to free up disk space. By default, Storage Sense is automatically turned on when the machine runs into low disk space and is set to run whenever the machine runs into storage pressure. This cadence can be changed in Storage settings or set with the "Configure Storage Sense cadence" group policy.
Enabled:Storage Sense is turned on for the machine, with the default cadence as ‘during low free disk space’. Users cannot disable Storage Sense, but they can adjust the cadence (unless you also configure the "Configure Storage Sense cadence" group policy)

Allow Storage Sense Temporary Files cleanup
Description: When Storage Sense runs, it can delete the user’s temporary files that are not in use.
Not Configured: By default, Storage Sense will delete the user’s temporary files. Users can configure this setting in Storage settings.

Configure Storage Sense Cloud Content dehydration threshold
Description: When Storage Sense runs, it can dehydrate cloud-backed content that hasn’t been opened in a certain amount of days.
Enabled - 30: You must provide the number of days since a cloud-backed file has been opened before Storage Sense will dehydrate it. Support values are: 0 - 365.  If you set this value to zero, Storage Sense will not dehydrate any cloud-backed content. The default value is 0, or never dehydrating cloud-backed content.

Configure Storage Storage Downloads cleanup threshold (yes, "Storage" is there two times as defined by Microsoft GPO)
Description: When Storage Sense runs, it can delete files in the user’s Downloads folder if they have been there for over a certain amount of days.
Enabled - 120: You must provide the minimum age threshold (in days) of a file in the Downloads folder before Storage Sense will delete it. Support values are: 0 - 365.  If you set this value to zero, Storage Sense will not delete files in the user’s Downloads folder. The default is 0, or never deleting files in the Downloads folder.

Configure Storage Sense cadence
Description: Storage Sense can automatically clean some of the user’s files to free up disk space.
Enabled - Every Month: You must provide the desired Storage Sense cadence. Supported options are: daily, weekly, monthly, and during low free disk space. The default is 0 (during low free disk space).

Configure Storage Sense Recycle Bit cleanup threshold
Description: When Storage Sense runs, it can delete files in the user’s Recycle Bin if they have been there for over a certain amount of days.
Not Configured: By default, Storage Sense will delete files in the user’s Recycle Bin that have been there for over 30 days. Users can configure this setting in Storage settings.

.NOTES
Released: 2020-06-11
Author: David McCleskey
Please note, these settings remain in the registry, and would require removal to revert Storage Sense back to default.  Removing the StorageSense Key provides a rollback to default.

.PREREQUISITES
  Windows 10 1809 (Redstone 5) or higher
#>

$RegKeyExists = 'HKLM:\Software\Policies\Microsoft\Windows\StorageSense'
$RegKeyPath = 'HKLM:\Software\Policies\Microsoft\Windows\'

try {
    New-Item -Path $RegKeyPath -Name 'StorageSense' -Force
    New-ItemProperty -Path $RegKeyExists -Name 'AllowStorageSenseGlobal' -Value '1' -PropertyType DWORD -Force
    New-ItemProperty -Path $RegKeyExists -Name 'ConfigStorageSenseCloudContentDehydrationThreshold' -Value '30' -PropertyType DWORD -Force
    New-ItemProperty -Path $RegKeyExists -Name 'ConfigStorageSenseDownloadsCleanupThreshold' -Value '120' -PropertyType DWORD -Force
    New-ItemProperty -Path $RegKeyExists -Name 'ConfigStorageSenseGlobalCadence' -Value '30' -PropertyType DWORD -Force
    Write-Output "SUCCESS: Storage sense Registry keys set"
}

catch { $Exception = $error[0].Exception.Message + "`nAt Line " + $error[0].InvocationInfo.ScriptLineNumber
            Write-Error $Exception
            exit 90
}