Monitoring with PowerShell: Monitoring Storage Sense settings

So let’s talk about Storage Sense. Storage Sense is a new-ish feature in Windows 10 which should replace the standard disk cleanup utilities. It has a lot more power than just disk cleanup as it can detect how long files have been in use and react based on age.

Storage Sense is pretty easy to use and can save a lot of disk space for modern clients, especially when they use OneDrive too. Storage Sense has support for OneDrive files on demand. By default it does not clear anything, but it could be setup to clear the local cache if files have not been in use for days, weeks, or even months. This helps with those pesky OneDrive database size limits.

So the first blog about Storage Senses will be monitoring its optimal settings. We’re using the RunAsUser Module for this, because the registry keys will be located in the HKEY_Current_User hive.

Monitoring script

#Settings
$PrefSched = 'Low Diskspace'
$ClearTemporaryFiles = $true
$ClearRecycler = $true
$ClearRecyclerDays = '60'
$ClearDownloads = $true
$ClearDownloadsDays = '60'
$AllowClearOneDriveCache = $true
#

If (Get-Module -ListAvailable -Name "RunAsUser") { 
    Import-module RunAsUser
}
Else { 
    Install-PackageProvider NuGet -Force
    Set-PSRepository PSGallery -InstallationPolicy Trusted
    Install-Module RunAsUser -force -Repository PSGallery
}


$ExpectedSettings = [PSCustomObject]@{
    'Storage Sense Enabled'         = $true
    'Clear Temporary Files'         = $ClearTemporaryFiles
    'Clear Recycler'                = $ClearRecycler
    'Clear Downloads'               = $ClearDownloads
    'Allow Clearing Onedrive Cache' = $AllowClearOneDriveCache
    'Storage Sense schedule'        = $PrefSched
    'Clear Downloads age (Days)'    = $ClearDownloadsDays
    'Clear Recycler age (Days)'     = $ClearRecyclerDays
} 

$ScriptBlock = {
    $StorageSenseKeys = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\StorageSense\Parameters\StoragePolicy\'

    $StorageSenseSched = switch ($StorageSenseKeys.'2048') {
        1 { 'Weekly' }
        7 { 'Every week' }
        30 { 'Every Month' }
        0 { 'Low Diskspace' }
        Default { 'Unknown - Could not retrieve.' }
    }

    [PSCustomObject]@{
        'Storage Sense Enabled'         = [boolean]$StorageSenseKeys.'01'
        'Clear Temporary Files'         = [boolean]$StorageSenseKeys.'04'
        'Clear Recycler'                = [boolean]$StorageSenseKeys.'08'
        'Clear Downloads'               = [boolean]$StorageSenseKeys.'32'
        'Allow Clearing Onedrive Cache' = [boolean]$StorageSenseKeys.CloudfilePolicyConsent
        'Storage Sense schedule'        = $StorageSenseSched
        'Clear Downloads age (Days)'    = $StorageSenseKeys.'512'
        'Clear Recycler age (Days)'     = $StorageSenseKeys.'256'
    } | ConvertTo-Json | Out-File "C:\windows\Temp\CurrentStorageSenseSettings.txt" -Force

 
}

$null = Invoke-AsCurrentUser -ScriptBlock $ScriptBlock -UseWindowsPowerShell -NonElevatedSession
$CurrentSettings = Get-Content  "C:\windows\Temp\CurrentStorageSenseSettings.txt" | ConvertFrom-Json

$ComparedObjects = Compare-Object $CurrentSettings $ExpectedSettings -Property $CurrentSettings.PsObject.Properties.name

if ($ComparedObjects) {
    Write-Host "Unhealthy - The Storage Sense settings are not the same as the expected settings."
}
else {
    write-host "Healthy - Storage Sense Settings are set correctly."
}

And that’s it. Next week I’ll be demonstrating how to setup Storage Sense automatically, including adding OneDrive sites. As always, Happy PowerShelling!

1 thought on “Monitoring with PowerShell: Monitoring Storage Sense settings

  1. Pingback: Automating with PowerShell: Deploying StorageSense - CyberDrain

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.