This script has been written to clear configuration cache from all SharePoint servers in the SharePoint farm. It makes a query against SharePoint farm to get list of all servers and leaves out mail and SQL server. After then, it proceeds to clear cache. This script has been commented throughout to clear how-its-working.

This script has been tested fine in SharePoint 2013 environment. Although, it has not been tested in SharePoint 2010 farm, but cache location, remains the same. So it should be able to run there too. However, it is always an best idea to run it first in your test environment and get familiar with the working of the script.

Assumptions:
1. This script assumes that current logged-on user has admin privileges on each SharePoint server in the farm.
2. You are running it from it an elevated PowerShell window.

3. Script takes pause of 1 min before proceeding to delete configuration cache on another SP server. It should be enough to re-generate xml files from config database. You can adjust this value as per your environment.

Note: Script has been updated to the take backup of cache folder before removing files from it. Now it first creates a backup folder with current date as suffix and copies existing cache folder inside it:

 

Here is the script code:

 

PowerShell
Edit|Remove
###################################################################################  
# Description: This script clears the configuration cache on the SharePoint servers  
#              in the sharepoint farm. This script has been tested for SharePoint  
#              2010 and SharePoint 2013 versions only.   
# Version    : 1.1  
# Written by : Mohit Goyal  
# Filename   : Clear-SPConfigCache.ps1  
###################################################################################  
 
 
#Load sharepoint module, just in case you are running this from normal PowerShell window 
Add-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 
 
#Fetches list of sharepoint servers in the farm. 
Write-Output "Fetching list of servers for this farm..." 
$Servers = Get-SPServer | Where-Object {$_.Role -eq "Application"} 
Write-Output "SharePoint servers are: $Servers" 
 
#Run following actions against each server in the farm 
Foreach ($Server in $Servers) 
{ 
    #Fetches the individual server name from the list and inform user 
    $ServerName = $Server.address 
    Write-Output "" 
    Write-Output "Performing action on server: $ServerName" 
 
    #Stops timer service 
    Write-Output "Stopping timer service..." 
    Get-Service SPTimerV4 -ComputerName $ServerName | Stop-Service 
 
    #Gets list of folders in location which contains cache.ini file 
    Write-Output "Proceeding to clear cache on the server..." 
    $folders = Get-ChildItem "\\$ServerName\C$\ProgramData\Microsoft\SharePoint\Config"  
    foreach ($folder in $folders) 
    { 
        $items = Get-ChildItem $folder.FullName -Recurse 
        foreach ($item in $items) 
        { 
            if ($item.Name.ToLower() -eq "cache.ini") 
            { 
                $cachefolder = $folder.FullName 
            } 
                 
        } 
    } 
     
    #If folder contains cache.ini file, it contains configuration cache. Proceed with further actions. 
    #TakeBackup of existing folder items 
    $Destination = $Cachefolder + "_" + ($(Get-Date).ToShortDateString().Replace("/","_")) 
    New-Item -Path $Destination -ItemType Directory -Force | Out-Null 
    Copy-Item -Path $cachefolder -Destination $Destination -Recurse 
 
    #Grab all .xml files and delete them 
    $cachefolderitems = Get-ChildItem $cachefolder -Recurse 
    foreach ($cachefolderitem in $cachefolderitems) 
    { 
         if ($cachefolderitem -like "*.xml") 
         { 
              $cachefolderitem.Delete() 
         } 
         
    } 
     
    #Set value of cache.ini to 1 
    Write-Output "Setting value of cache.ini to 1..." 
    $a = Get-Content  $cachefolder\cache.ini 
    $a  = 1 
    Set-Content $a -Path $cachefolder\cache.ini 
 
    #Starting timer service 
    Write-Output "Starting timer service..." 
    Get-Service SPTimerV4 -ComputerName $ServerName | Start-Service 
 
    #Halt execution for 1 minute so that xml files can be re-generated fine 
    Write-Output "Waiting for 1 min..." 
    Sleep 60 
 
} 
 
Write-Host "`nScript Execution finished" 
 
################################################################################### 
# End of Script  
###################################################################################