This script is to clear the SharePoint config cache from all the application servers. It can be run from any application sever. Bascially it fetches the cache folder guid from registry and connects to each application server and performs the required task.

Here are the steps at high level

1. Fetches folder guid and checks for folder existance in each application server

2. Stops the SPTimer job from all servers

3. Deletes the files and replaces the content in Cache.ini with 1

4. Starts the SPTimer job on all application servers.

 

Note: For SP2016 the SPServer Role names are changed. $servers = Get-SPServer | ?{$_.Role -ne "Invalid"} 

PowerShell
Edit|Remove
Add-PSSnapin -Name Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue 
 
Write-Host "Clear SharePoint config cache on farm" -f Green 
$servers = Get-SPServer | ?{$_.Role -eq "Application"# $servers = Get-SPServer | ?{$_.Role -ne "Invalid"} SP2016 
 
#Find the cache folder guid from registry from anyone of the application server 
#This path is for SharePoint 2013.. may change in future versions of SharePoint 
$registryPath = "HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\15.0\Secure\ConfigDB" 
$folderGuid = (Get-ItemProperty -Path $registryPath).Id 
Write-Host "Cache folder path is.." -f Cyan 
Write-Host "\\<servername>\C$\ProgramData\Microsoft\SharePoint\Config\$folderGuid" -f Cyan 
#Find the cache folder path in all application servers 
foreach ($server in $servers) 
{ 
    $servername = $server.Address 
    $cacheFolderPath = "\\$servername\C$\ProgramData\Microsoft\SharePoint\Config\$folderGuid" 
     
    $cacheFolderItems = Get-ChildItem ($cacheFolderPath) 
    #check if cache.ini is present 
    if(($cacheFolderItems | ?{$_.Name.ToLower() -eq "cache.ini"}).Count -eq 1) { 
        Write-Host "Found cache folder on Server $servername" -f Yellow 
    } 
    else { 
        Write-Host "Cache folder not found on Server $servername .. Something may go wrong. Please verify.." -f Red 
        exit 
    } 
} 
 
Write-Host "Stop Timer Service on each server" -f Cyan 
foreach ($server in $servers) 
{ 
    $servername = $server.Address 
    Write-Host "$servername" -f Yellow 
    Get-Service SPTimerV4 -ComputerName $ServerName | Stop-Service  
} 
 
Start-Sleep -s 15 # to ensure the timer jobs are stopped 
 
Write-Host "Deleting all XML files inside the cache folder on each server..." -f Cyan 
foreach ($server in $servers) 
{ 
    $servername = $server.Address 
    $cacheFolderPath = "\\$servername\C$\ProgramData\Microsoft\SharePoint\Config\$folderGuid" 
    Write-Host "$servername" -f Yellow 
    #extension literal is case insenstive.. so it works even for *.XML     
    #Get-ChildItem $cacheFolderPath -Include "*.xml" -Recurse | Remove-Item 
    $cachefolderitems = Get-ChildItem $cacheFolderPath -Recurse  
    foreach ($cachefolderitem in $cachefolderitems)  
    {  
         if ($cachefolderitem -like "*.xml")  
         {  
              $cachefolderitem.Delete()  
         }  
          
    }  
 
    #$fileContent = Get-Content $cacheFolderPath\cache.ini 
    $fileContent = 1 
    Write-Host "Replace content in Cache.ini File on server $servername" -f Cyan     
    Set-Content $fileContent -Path $cacheFolderPath\cache.ini 
} 
 
Write-Host "Start Timer Service on each server" -f Cyan 
foreach ($server in $servers) 
{ 
    $servername = $server.Address 
    Write-Host "$servername" -f Yellow 
    Get-Service SPTimerV4 -ComputerName $ServerName | Start-Service  
    #(Get-WmiObject Win32_Service -filter "name='SPTimerV4'" -ComputerName $servername).StartService() | Out-Null 
}
 
Code Snippet for IISReset on all application servers
PowerShell
Edit|Remove
$spserver = Get-SPserver | ?{$_.Role -eq "Application"}  
foreach ($server in $spserver)  
{ 
   Write-Host "Performing IISReset on Server: " $server.name 
   IISReset $server.Name  
}