free up space by deleting temp files via intune

Super Contributor

Hi all,

 

Is it possible to delete files which are possible to delete to get more space on devices via intune or scripting? 

 

 

 

 

15 Replies
Yes this is possible via a simple endpoint analytics remediation script. Do you have specific folders wo want to delete?
If you want I can provide you the script.

As an alternative, you can also consider configuring Storage Sense, as described on Manage drive space with Storage Sense (microsoft.com). Among other things, this allows you to (automatically) clean up temporary files when disk space runs low. 

 

Also, check out this blog on how to configure it with Settings Catalog: 

How To Configure Storage Sense Using Intune | Endpoint Manager HTMD Blog (anoopcnair.com)

 

Yes please!

App data local & roaming
Any other system files that take up space really do you have any in mind?
Hi I have already done this but no luck with taking off much space on most devices

Jannik his option is indeed a good one... combining it with storage sense... is also a smart thing to do... but still we are using our own scripting... :) almost the same policy we used on our remote desktop environments

 

This script will make sure data in all the users temp folders will be deleted. You can simply add stuff you need

Write-Host -ForegroundColor Green "Getting the list of users"
md c:\install -force
dir C:\Users | select Name | Export-Csv -Path C:\install\users.csv -NoTypeInformation
$list=Test-Path C:\install\users.csv


Write-Host -ForegroundColor Green "SECTION 1: Clearing some other stuff"

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
Remove-item C:\Users\$($_.Name)\AppData\Local\Microsoft\Outlook\*.nst -Force -EA SilentlyContinue -Verbose
Remove-item C:\Users\$($_.Name)\appdata\Local\Microsoft\Windows\Explorer\*.db -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Teams\previous\*" -Recurse -Force -EA SilentlyContinue -Verbose
}


Write-Host -ForegroundColor Green "SECTION 2: Clearing Mozilla Firefox Caches"

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*.* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache2\entries\*.* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\thumbnails\* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cookies.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\webappsstore.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\chromeappsstore.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
Remove-item C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*\Cache2\* -Recurse -Force -EA SilentlyContinue -Verbose
}


Write-Host -ForegroundColor Green "SECTION 3: Clearing Google Chrome Caches"

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cache2\entries\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cookies" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Media Cache" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Service Worker\CacheStorage\*" -Recurse -Force -EA SilentlyContinue -Verbose
}


Write-Host -ForegroundColor Green "SECTION 4: Clearing Internet Explorer Caches"

Import-CSV -Path C:\install\users.csv | foreach {
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\WER\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\inetcache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\webcache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Windows\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\`$recycle.bin\" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Edge\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Edge\User Data\Default\Code Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Edge\User Data\Default\Service Worker\CacheStorage\*" -Recurse -Force -EA SilentlyContinue -Verbose
}

@Rudy_Ooms_MVP @Jannik_Reinhard 

 

Thank you both, I will try both of these and keep you posted. 

@AB21805: You can combine both. You can add the cleanup script from @Rudy_Ooms_MVP to my remediation script.
Hi, so would I literally add it under your part of the script?
Exactly only add this to may remediation script below the disk cleanup part.

Hi@Jannik_Reinhard 

So like this:

 

<#
Version: 1.0
Author: Jannik Reinhard (jannikreinhard.com)
Script: Get-CleanUpDiskRemediation
Description:
Cleanup disk when utilization <15GB
Release notes:
Version 1.0: Init
#>

$cleanupTypeSelection = 'Temporary Sync Files', 'Downloaded Program Files', 'Memory Dump Files', 'Recycle Bin'

foreach ($keyName in $cleanupTypeSelection) {
$newItemParams = @{
Path = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$keyName"
Name = 'StateFlags0001'
Value = 1
PropertyType = 'DWord'
ErrorAction = 'SilentlyContinue'
}
New-ItemProperty @newItemParams | Out-Null
}

Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -NoNewWindow -Wait
Write-Host -ForegroundColor Green "Getting the list of users"
md c:\install -force
dir C:\Users | select Name | Export-Csv -Path C:\install\users.csv -NoTypeInformation
$list=Test-Path C:\install\users.csv

Write-Host -ForegroundColor Green "SECTION 1: Clearing some other stuff"

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
Remove-item C:\Users\$($_.Name)\AppData\Local\Microsoft\Outlook\*.nst -Force -EA SilentlyContinue -Verbose
Remove-item C:\Users\$($_.Name)\appdata\Local\Microsoft\Windows\Explorer\*.db -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Teams\previous\*" -Recurse -Force -EA SilentlyContinue -Verbose
}


Write-Host -ForegroundColor Green "SECTION 2: Clearing Mozilla Firefox Caches"

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache\*.* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cache2\entries\*.* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\thumbnails\* -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\cookies.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\webappsstore.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*.default\chromeappsstore.sqlite -Recurse -Force -EA SilentlyContinue -Verbose
Remove-item C:\Users\$($_.Name)\AppData\Local\Mozilla\Firefox\Profiles\*\Cache2\* -Recurse -Force -EA SilentlyContinue -Verbose
}


Write-Host -ForegroundColor Green "SECTION 3: Clearing Google Chrome Caches"

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cache2\entries\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cookies" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Media Cache" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Cookies-Journal" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Google\Chrome\User Data\Default\Service Worker\CacheStorage\*" -Recurse -Force -EA SilentlyContinue -Verbose
}


Write-Host -ForegroundColor Green "SECTION 4: Clearing Internet Explorer Caches"

Import-CSV -Path C:\install\users.csv | foreach {
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\Temporary Internet Files\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\WER\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\inetcache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Windows\webcache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Windows\Temp\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\`$recycle.bin\" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Edge\User Data\Default\Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Edge\User Data\Default\Code Cache\*" -Recurse -Force -EA SilentlyContinue -Verbose
Remove-Item -path "C:\Users\$($_.Name)\AppData\Local\Microsoft\Edge\User Data\Default\Service Worker\CacheStorage\*" -Recurse -Force -EA SilentlyContinue -Verbose
}

Hi @Jannik_Reinhard 

 

I am getting failed: 

 

Screenshot 2022-07-20 at 10.14.24 AM.png

 

Any reason why? 

 

 

Can you execute this scrip manually on a device and check what is the output?

Hi @Jannik_Reinhard and @Rudy_Ooms_MVP,

Thanks for sharing this.

I am keen to try it too.

 

However, I have machines running in Kiosk mode (Windows 10 Multi Kiosk Profile deployed by Intune).

Would either the method (Storage Sense or Proactive remediations with your scripts) work under the user-less context? Are those related Windows CSP device or user scoped?

 

Me reading the Storage Sense Doc, it keeps saying "Deleting files for users", etc.

 

Thank you.