This is a follow up blog on last weeks blog of monitoring Storage Sense settings; to read about that check out the previous blog here. Monitoring Storage sense can ease your maintenance workload, but you do need a way to deploy StorageSense too.
You can use the script below to deploy storagesense, it even gives you the option to automatically add the OneDrive sites to the StorageSense settings, It’s important to note that StorageSense does not remove cache files that are marked as ‘Always remain offline’ – It only clears the cache for files that are untouched as long as you’ve defined.
Alright, so let’s go to deploying; as before the script uses my RunAsUser module because the StorageSense settings are user based.
The script
Using the script is straightforward; change the deployment settings to your preference, and run the script below.
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
49
|
#Settings
[PSCustomObject]@{
PrefSched = '0' #Options are: 0(Low Diskspace),1,7,30
ClearTemporaryFiles = $true
ClearRecycler = $true
ClearDownloads = $true
AllowClearOneDriveCache = $true
AddAllOneDrivelocations = $true
ClearRecyclerDays = '60' #Options are: 0(never),1,14,30,60
ClearDownloadsDays = '60' #Options are: 0(never),1,14,30,60
ClearOneDriveCacheDays = '60' #Options are: 0(never),1,14,30,60
} | ConvertTo-Json | Out-File "C:\Windows\Temp\WantedStorageSenseSettings.txt"
#
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
}
$ScriptBlock = {
$WantedSettings = Get-Content "C:\Windows\Temp\WantedStorageSenseSettings.txt" | ConvertFrom-Json
$StorageSenseKeys = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\StorageSense\Parameters\StoragePolicy\'
Set-ItemProperty -Path $StorageSenseKeys -name '01' -value '1' -Type DWord -Force
Set-ItemProperty -Path $StorageSenseKeys -name '04' -value $WantedSettings.ClearTemporaryFiles -Type DWord -Force
Set-ItemProperty -Path $StorageSenseKeys -name '08' -value $WantedSettings.ClearRecycler -Type DWord -Force
Set-ItemProperty -Path $StorageSenseKeys -name '32' -value $WantedSettings.ClearDownloads -Type DWord -Force
Set-ItemProperty -Path $StorageSenseKeys -name '256' -value $WantedSettings.ClearRecyclerDays -Type DWord -Force
Set-ItemProperty -Path $StorageSenseKeys -name '512' -value $WantedSettings.ClearDownloadsDays -Type DWord -Force
Set-ItemProperty -Path $StorageSenseKeys -name '2048' -value $WantedSettings.PrefSched -Type DWord -Force
Set-ItemProperty -Path $StorageSenseKeys -name 'CloudfilePolicyConsent' -value $WantedSettings.AllowClearOneDriveCache -Type DWord -Force
if ($WantedSettings.AddAllOneDrivelocations) {
$CurrentUserSID = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value
$CurrentSites = Get-ItemProperty 'HKCU:\SOFTWARE\Microsoft\OneDrive\Accounts\Business1\ScopeIdToMountPointPathCache' -ErrorAction SilentlyContinue | Select-Object -Property * -ExcludeProperty PSPath, PsParentPath, PSChildname, PSDrive, PsProvider
foreach ($OneDriveSite in $CurrentSites.psobject.properties.name) {
New-Item "$($StorageSenseKeys)/OneDrive!$($CurrentUserSID)!Business1|$($OneDriveSite)" -Force
New-ItemProperty "$($StorageSenseKeys)/OneDrive!$($CurrentUserSID)!Business1|$($OneDriveSite)" -Name '02' -Value '1' -type DWORD -Force
New-ItemProperty "$($StorageSenseKeys)/OneDrive!$($CurrentUserSID)!Business1|$($OneDriveSite)" -Name '128' -Value $WantedSettings.ClearOneDriveCacheDays -type DWORD -Force
}
}
}
$null = Invoke-AsCurrentUser -ScriptBlock $ScriptBlock -UseWindowsPowerShell -NonElevatedSession
|
And that’s it! as always, Happy PowerShelling!