PowerShell Playground - Clear Cookies

Have you ever needed to clear the cookies on multiple computers? Well, as long as you have administrative priveledges on all of the machines, you can use this script I cooked up to do just that. The computer names can be indevidually typed in by hand, or you can supply a list of computernames and you will be good to go from there.

The following script is written in powershell.

# ========================================================================
# Function Name 'get-OSVersion' - Retrieves the Base operating system number
# ========================================================================

Function Get-OSVersion{
    Param([string]$Computer = "LocalHost")
    $OS = Get-WmiObject -class Win32_OperatingSystem -computer $Computer
    $Split = $OS.version.split(".")
    $Version = $split[0] + "." + $split[1]
    Return $Version
}

# ========================================================================
# Function Name 'delete-cookies' - Delete all cookies from all users on a windows xp machine
# ========================================================================
function Delete-cookies($cookiecomputer){
    $OSversion = Get-OSVersion $cookiecomputer
    $ostype = $null
    if($OSVersion -eq "5.1"){
     $docspath = "$cookiecomputer" +'c$documents and settings'
        $ostype = "old"
        Tee-LogOutput "XP OS detected for $cookiecomputer using $docspath"
        $cookiePath = "Cookies"
 }
    elseif(($OSVersion -eq "6.0") -or ($OSVersion = "6.1")){
     $docspath = "$cookiecomputer" +'c$users'
        $ostype = "new"
        $cookiePath = "AppDataRoamingMicrosoftWindowsCookies"
        Tee-LogOutput "Vista or Windows 7 detected for $cookiecomputer using $docspath"
 }
    else{
        $docspath = "$cookiecomputer" +'c$users'
        Tee-LogOutput "OS not detected for $cookiecomputer using Default of $docspath"
        $ostype = "new"
        $cookiePath = "AppDataRoamingMicrosoftWindowsCookies"
    }
    $UserFolders = get-childitem $docspath | where {$_.psIsContainer -eq $true}
 Foreach($folder in $userfolders){
  if($folder.name -eq "all users"){Tee-LogOutput "skipping `"All Users`" Folder"}
  else{
   $cookies = get-childitem $docspath$folder$CookiePath | where {$_.extension -eq ".txt"}
            Tee-LogOutput "will delete all files in $docspath$folder$CookiePath"
            If($cookies){
    foreach($cookie in $cookies){
     $strcookie = $cookie.name.replace("[","``[")
     $strcookie = $StrCookie.replace("]","``]")
     Tee-LogOutput "Deleting Cookie -- $docspath$folder$CookiePath$strcookie"
     # capture errors for logging
     remove-item "$docspath$folder$CookiePath$strcookie" 2>> $logfile
    }
   }
  }
 }
 CreateTimestamp($docspath)
}


# ========================================================================
# Function Name 'CreateTimestamp' - Creates a text file containing the Current date
# ========================================================================
function Createtimestamp($timestampLocation){
 $cookiepath = "$timestamplocation" + 'cookieClearDate.txt'
 $date = get-date -uformat "%Y%m%d"
 $file = New-Item -ItemType File $cookiepath -force
 add-content $file $date
}

 

# ========================================================================
# Function Name 'ListTextFile' - Enumerates Computer Names in a text file
# Create a text file and enter the names of each computer. One computer
# name per line. Supply the path to the text file when prompted.
# ========================================================================
Function ListTextFile {
 $strText = Read-Host "Enter the path for the text file"
 $complist = Get-Content $strText
 
 Tee-LogOutput "The following computers will be Cleared"
}

# ========================================================================
# Function Name 'SingleEntry' - Enumerates Computer from user input
# ========================================================================
Function ManualEntry {
 $complist = Read-Host "Enter Computer Name or IP"
}

# ========================================================================
# Function Name 'converuserdate' Will convert a date from MMDDYYYY format to YYYYMMDD Format
# ========================================================================
Function ConvertuserDate($odate){
 $month = $odate.substring(0,2)
 $day = $odate.Substring(2,2)
 $year= $odate.substring(4)
 $convertedInput = "$year$Month$Day"
 $convertedInput
}

# =======================================================================
# Funtion Name 'Tee-LogOutput' will send $string to the logfile as well as output to display
# =======================================================================
Function Tee-LogOutput($String){
 write-Host $string
 add-content $LogFile $String
}


# =========================================
# Main script Start
# =========================================

#setup Log file
$logfile = New-Item -ItemType File c:logsCookieScriptLog.txt -force

#
# Gather user Input
write-Host "This script will clear the cookies for all users on a single, or list of windows xp computers." -ForegroundColor Green
write-Host ""

write-Host "Would you like to enforce a last run Cuttoff Date" -ForegroundColor Green
$strResponse = Read-Host "[1] Yes, [2] No"
If($strResponse -eq "1"){$cutoffDate = read-host "This will Clear cookies on given computers only if they have been last cleared earlier than the Given date... Enter CutoffDate in MMDDYYYY format"}
 elseif($strResponse -eq "2"){$cutoffDate = 0}
 else{write-Host "You did not supply a correct response, Please run script again." -foregroundColor Red}


write-Host "How would you like to input Computer list to be cleaned" -ForegroundColor Green
$strResponse = Read-Host "[1] Computer names from a File, [2] Choose a Computer manually"
If($strResponse -eq "1"){. ListTextFile}
 elseif($strResponse -eq "2"){. ManualEntry}
 else{write-Host "You did not supply a correct response, Please run script again." -foregroundColor Red}
write-Host ""
write-Host ""
write-Host "Please confirm the input you have provided"
Read-host "if the information is not correct, Close this window and start again - press enter to continue with the replace"
foreach($tcomputer in $complist){
 $date = get-date
 tee-logoutput "Starting work on $tcomputer at $date"
 $islive = test-connection -computername $tcomputer -ttl 5 -count 1
 if($islive){
  If($cutoffdate -ne "0"){
   $convertedDate = convertUserdate($cutoffdate)
   $datestamppath = "$tcomputer" +'c$documents and settingscookieclearDate.txt'
   $datestamp = Get-content $dateStamppath 2>> $logfile
   If($dateStamp){
    Tee-LogOutput "Date Stamp found"
    if($datestamp -lt $convertedDate){
     Tee-LogOutput "Cutoffdate exceeded Cleaning $tcomputer"
     Delete-cookies($tcomputer)
    }
    Else{
     Tee-LogOutput "$tcomputer is within Cutoff Threshold.. Skipping"
    }
   }
   Else{
    Tee-LogOutput "Date Stamp file not found.. Cleaning $tcomputer"
    Delete-cookies($tcomputer)
   }
  }
  Else{
   Tee-LogOutput "Cutoff not used cleaning $tcomputer"
   Delete-cookies($tcomputer)
  }
  $date = get-date
  tee-logoutput "completing work on $tcomputer at $date"
  tee-logoutput " "
  tee-logoutput " "
 }
 else{
  $date = get-date
  tee-logoutput "$tcomputer is not responding at $date... Skipping"
  tee-logoutput " "
  tee-logoutput " "
 }
}

Read-host "Script Completed - Press enter to continue"

Please give it a shot.