Clear cache folder from Users appdata (onenote)

Bronze Contributor

Hi all

 

I have a script which clears files in the cache folder which works if I run on the laptop manually however I cant seem to get it done via intune? 

 

Here is the script: 

 

 # Define the path to the cache directory

$cachePath = "C:\Users\$env:USERNAME\AppData\Local\Microsoft\OneNote\16.0\cache"

 

# Get all files and folders in the cache directory

$items = Get-ChildItem -Path $cachePath -Force -Recurse

 

# Iterate through each item

foreach ($item in $items) {

    # Check if it's a file or a directory

    if ($item.PSIsContainer) {

        # Remove the directory and all its contents

        Remove-Item -Path $item.FullName -Force -Recurse

    } else {

        # Remove the file

        Remove-Item -Path $item.FullName -Force

    }

}

 

 

6 Replies

@AB21805 How are you deploying it? As a script within devices? (Devices - Microsoft Intune admin center) ? And did you select "Run this script using the logged on credentials" ?

Yes, I did using logged in credentials. I ideally need this run everyday.

I have tried doing it as a script with devices but as user log in details and as a remediation script

Please help!

@AB21805 You can run it as remediation script every day for the logged in user, but do you have a detection script to trigger it?

Yes I do however the script comes back as "without Issues" so that is why I tried the actual script via scripts instead to see if it works however it doesnt but it works if I run the script manually on the devices.

Detection script:
# Define the path to the cache directory
$cachePath = "C:\Users\$env:USERNAME\AppData\Local\Microsoft\OneNote\16.0\cache"

# Check if there are any files in the cache directory
$files = Get-ChildItem -Path $cachePath -File
if ($files.Count -gt 0) {
Write-Output "Files found in cache directory"
Write-Output "Non-Compliant"
}
else {
Write-Output "No files found in cache directory"
Write-Output "Compliant"
}
I would add an exit to it..... and myabe if you want to perform the delete from the system context instead of the user , you could do something like this

detect

-------------------------
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

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
$files = get-childitem C:\Users\$($_.Name)\AppData\Local\Microsoft\OneNote\16.0\cache -Force -EA SilentlyContinue -Verbose
}

if ($files.Count -gt 0) {
Write-Output "Files found in cache directory"
Write-Output "Non-Compliant"
exit 1
}
else {
Write-Output "No files found in cache directory"
Write-Output "Compliant"
exit 0
}



remediate
-----------------
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

Import-CSV -Path C:\install\users.csv -Header Name | foreach {
Remove-item C:\Users\$($_.Name)\AppData\Local\Microsoft\OneNote\16.0\cache\*.* -Force -EA SilentlyContinue -Verbose
}
I will give this a go thanks Rudy, I will keep you posted.