LumifyClassroomCleanup.psm1

function Remove-BrowserCache {
  <#
  .SYNOPSIS
    This will remove internet browers' cached user data
  .DESCRIPTION
    This script removes all of the user data stored by the browser/s, It first stops
    all of the current browser processes and then removes the cached user's data.
    This script will remove the cached data from four types of browsers if none are
    explicitly chosen with the paramater -BrowserType
    parameter.
  .Parameter BrowserType
    This parameter controls which of the four browsers to remove the cached data from
    the options are Chrome, Firefox, MSEdge and IExplore.
    Multiple browser types can be entered as an array seperated by commas.
  .EXAMPLE
    Remove-BrowserCache
    Deletes the user data from Chrome, Firefox, Edge and Internet Explorer
    (the default targets all four browsers)
  .EXAMPLE
    Remove-BrowserCache -BrowserType IExplore,Chrome
    Deletes the user data from Chrome and IE only
  .EXAMPLE
    Remove-BrowserCache -BrowserType FireFox
    Deletes the user data from FireFox only
  .EXAMPLE
    Remove-BrowserCache -BrowserType MsEdge, IExplore, Chrome
    Deletes the user data from Edge, Internet Explorer and Chrome only
  .NOTES
    General notes
    Created by: Brent Denny
    Created on: 9 Aug 2019
    Updated on: 14 Apr 2023
 
    Version History:
    0.0.1 - 9 Aug 2019 - Initial version for trail
    1.0.0 - 13 Apr 2023 - Version that also incorporates Edge
    1.0.1 - 14 Apr 2023 - Updated the help to better reflect the new features
  #>

  [CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
  Param(
    [ValidateSet('Chrome','FireFox','IExplore','MSEdge')]
    [string[]]$BrowserType = @('Chrome','FireFox','IExplore','MSEdge')
  )
  if ($PSCmdlet.ShouldProcess(($BrowserType -join ' and '), "Terminating processes")) {
    Write-Verbose 'Killing all current brower sessions, hopefully?'
    Get-Process | Where-Object {$_.ProcessName -in $BrowserType} | Stop-Process -Force
    $Counter = 0
    do {
      Start-Sleep -Seconds 1
      $Counter++
      $BrowserProcs = Get-Process | Where-Object {$_.ProcessName -in $BrowserType}
    } until ($BrowserProcs.Count -eq 0 -or $Counter -eq 20)
    if ($BrowserProcs.Count -ne 0) {
      Write-Warning "The selected browsers did not terminate in a timely fashion, `nplease close the browsers manually and re-run the script"
      break
    }
  }
  switch ($BrowserType) {
    {$_ -contains 'Chrome'} {
      if ($PSCmdlet.ShouldProcess('Chrome', "Delete User Data")) {
        $ChromePath = $env:LOCALAPPDATA + "\Google\Chrome\User Data\*"
        if (Test-Path ($env:LOCALAPPDATA + "\Google\Chrome\User Data")) {
          try {
            Write-Verbose 'Attempting to clear user data from Chrome'
            Remove-Item -Path $ChromePath -Recurse -Force -ErrorAction stop
          }
          catch {Write-Warning 'Cannot delete the Chrome user data'}
        }
        else {Write-Verbose 'No user data exists for the Chrome browser'}
      }
    }
    {$_ -contains 'Firefox'}  {
      if ($PSCmdlet.ShouldProcess('Firefox', "Delete User Data")) {
        if (Test-Path $env:APPDATA\Mozilla\Firefox\Profiles) {
          $FirefoxProfileFolders = (Get-ChildItem $env:APPDATA\Mozilla\Firefox\Profiles\ -Directory).FullName
          Try {
            Write-Verbose 'Attempting to clear user data from Chrome Firefox'
            foreach ($ProfDir in $FirefoxProfileFolders) {
              Remove-Item -Recurse -Force -Path $ProfDir\* -ErrorAction stop
            }
          }
          Catch {Write-Warning 'Cannot delete the Firefox user data'}
        }
        else {Write-Verbose 'No user data exists for the Firefox browser'}    
      }
    }
    {$_ -contains 'IExplore'} {
      if ($PSCmdlet.ShouldProcess('Internet Explorer', "Delete User Data")) {
        Write-Verbose 'Attempting to clear user data from Internet Explorer'
        invoke-command -ScriptBlock {RunDll32.exe InetCpl.cpl, ClearMyTracksByProcess 255}
      }
    }
    {$_ -contains 'MSEdge'} {
      if ($PSCmdlet.ShouldProcess('Edge', "Delete User Data")) {
        Write-Verbose 'Attempting to clear user data from Edge'
        try {
          if (Test-Path "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default") {
            Remove-Item "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default" -Recurse -Force -ErrorAction Stop
          }
        }
        catch {Write-Warning 'Cannot delete the Edge user data'}
      }
    }    
    Default {Write-Warning 'Failed to clear user data, can only clear user data from Chrome, Firefox, Edge and Internet Explorer'}
  }
}