Forum Discussion
tsmith92765
Jan 13, 2023Copper Contributor
Creating a Script to Delete Cookies and Cache of FireFox but if-else statements are not working.
Hello PS Community! I'm currently in the process of making a script for my company to delete Cookies and Cache of the 3 main browsers we use. Firefox, Chrome, and Edge. My script works perfectly ...
- Jan 14, 2023
tsmith92765 I changed your script a little bit:
$FireFox = { # Create a User Variable for the ScriptBlock to use # $user = (Get-WMIObject -ClassName Win32_ComputerSystem | Select-Object username).username.split("\")[1] # Remove Cookies from FireFox # if (Test-Path C:\Users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\*.default-release\cookies.sqlite) { Remove-Item -path C:\Users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\*.default-release\cookies.sqlite -ErrorAction SilentlyContinue Write-Host "FireFox Cookies have been found and cleaned." } else { Write-Host "FireFox Cookies not found." } # Remove Cache from Firefox # if (Test-Path -path C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default-release\cache2) { Remove-Item -path C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default-release\cache2 -recurse -ErrorAction SilentlyContinue Write-Host "Firefox Cache has been found and cleaned." } else { # If no cookies found print a msg # Write-Host "FireFox Cache not found." } } invoke-command -scriptblock $FirefoxThis works on my Firefox installation. The first time it cleans, and the second time it can't find the items anymore which is correct. Could you verify this?
Jan 14, 2023
tsmith92765 I changed your script a little bit:
$FireFox = {
# Create a User Variable for the ScriptBlock to use #
$user = (Get-WMIObject -ClassName Win32_ComputerSystem | Select-Object username).username.split("\")[1]
# Remove Cookies from FireFox #
if (Test-Path C:\Users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\*.default-release\cookies.sqlite) {
Remove-Item -path C:\Users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\*.default-release\cookies.sqlite -ErrorAction SilentlyContinue
Write-Host "FireFox Cookies have been found and cleaned."
}
else {
Write-Host "FireFox Cookies not found."
}
# Remove Cache from Firefox #
if (Test-Path -path C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default-release\cache2) {
Remove-Item -path C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default-release\cache2 -recurse -ErrorAction SilentlyContinue
Write-Host "Firefox Cache has been found and cleaned."
}
else {
# If no cookies found print a msg #
Write-Host "FireFox Cache not found."
}
}
invoke-command -scriptblock $Firefox
This works on my Firefox installation. The first time it cleans, and the second time it can't find the items anymore which is correct. Could you verify this?
tsmith92765
Jan 16, 2023Copper Contributor
Harm_Veenstra Hey, I just tried the script, and it worked flawlessly. Thank you so much for your help!!! I was just thinking "what if there is a way to test if the file is present before continuing on with the script" and sure enough!
- Jan 16, 2023No problem, glad to help!