Forum Discussion

tsmith92765's avatar
tsmith92765
Copper Contributor
Jan 13, 2023
Solved

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 fine with Chrome and Edge. So, I copied the basic formula for it with Firefox but for some reason my if and else statements aren't working as I had planned. 

 

The Below is the ScriptBlock in Question:

$FireFox = {
    # Create a User Variable for the ScriptBlock to use #
    $user = (Get-WMIObject -ClassName Win32_ComputerSystem | select username).username.split("\")[1]

    # Remove Cookies from FireFox #
    Remove-Item -path C:\Users\$user\AppData\Roaming\Mozilla\Firefox\Profiles\*.default-release\cookies.sqlite -EA SilentlyContinue -ErrorVariable CookieError;

    # If no cookies found print a msg #
    if($CookieError){
        Write-Host "FireFox Cookies not found."
    }
    # Else if cookies found print a msg #
    else {
        Write-Host "FireFox Cookies have been found and cleaned."
    }

    # Remove Cache from Firefox #
    Remove-Item -path C:\Users\$user\AppData\Local\Mozilla\Firefox\Profiles\*.default-release\cache2 -recurse -EA SilentlyContinue -ErrorVariable CacheError;

    # If no cookies found print a msg #
    If($CacheError){
        Write-Host "FireFox Cache not found."
    }
    # Else if cookies found print a msg #
    else {
        Write-Host "Firefox Cache has been found and cleaned."
    }
}

invoke-command -scriptblock $Firefox

 

This script is ran remotely, and everytime it runs whether they have a cache or cookies, it'll always print out "Firefox Cache/Cookies has/have been found and cleaned." even when there is nothing there.

 

For my Google/Edge Scriptblocks they'd error out if no file was found and cause the script to print out "Cookies/Cache not found".

 

It doesn't seem like the ErrorVariable is picking up an error at all and is staying empty, all the time.

 

Any advice would be greatly appreciated.

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

3 Replies

  • 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's avatar
      tsmith92765
      Copper 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! 

Resources