SharePoint 2013 Warmup Scripts using PowerShell

What is “Warmup Scripts” in SharePoint?
By default, IIS application pools recycle every night to keep clean memory space –  Every time the IIS Application pool recycles – Asp.net assemblies to be re-compiled to serve the page to end-user. So, When the recycling occurs during mid-night (or when you do manual recycling/IISReset), The very first user types the URL experiences a wait time of 30 seconds to 120 seconds on average. (again, this depends on your hardware-software configurations!). But the subsequent requests come faster from the server for the same site!

So, the idea is: “warm up” the site before users start requesting it so that they don’t suffer at first time hit.  Warm up scripts triggers  requests to your servers regularly to “Warmup” IIS.

Ok, Where is the script? Which warm-up script I’ve to use? Well, there are many available! Here is my pick among them: https://github.com/spjeff/spbestwarmup and You may have to customize it based on your requirement. In my experience, This simple warm up script works amazing with all versions of SharePoint: SharePoint 2013, SharePoint 2010, and in SharePoint 2007:

Warmup PowerShell Script for SharePoint 2013

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function Get-WebPage([string]$url,[System.Net.NetworkCredential]$cred=$null) 
{ 
    $WebClient = new-object net.webclient 
    if($cred -eq $null) 
    { 
        $cred = [System.Net.CredentialCache]::DefaultCredentials; 
    } 
    $WebClient.credentials = $cred; 
    return $WebClient.DownloadString($url); 
} # end Function 

# Make sure the account has enough permission to read sites
$cred = [System.Net.CredentialCache]::DefaultCredentials; 
#If you need to use an another account
#$cred = new-object System.Net.NetworkCredential("USER ID","PASSWORD","DOMAIN")

#Get All Web Applications and iterate through
$WebAppsColl = Get-SPWebApplication -IncludeCentralAdministration

foreach($WebApp in $WebAppsColl)
{
    #Get All site collections and iterate through
    $SitesColl = $WebApp.Sites

    foreach ($Site in $SitesColl) 
    {
 Write-Host "Warming up Site collection:"  $Site.URL
 $html = Get-WebPage -url $Site.URL  -cred $cred 
    }

#Change these URL's if you need any other Site/URL for warmup
#$html = Get-WebPage -url "http://northwind.crescent.com/SitePages/Home.aspx" -cred $cred 
}

You’ll have to add each of your web application to the above list of URLs. For Search web application, You’ll have to send a Search query, such as: https://intranet.contoso.com/_layouts/OSSSearchResults.aspx?k=warmup

You have to edit your host files, so that Your Warmup script hits the same WFE server it’s running, instead of going to the load balancer and gets pages from any other web server.

Warm up Script for Host-Named Site Collections:

Let’s add a bit more enhancement to it:

  • Let’s keep a log for warmed up sites, 
  • Warm-up all Host-named site collections – Let’s create a proxy to hit on the SAME web front end rather than going to load balancer – We can’t edit the HOST file for each HNSC, isn’t it?
start-transcript -path C:\Scripts\$(get-date -format 'ddMMyyyy').txt -append

Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue  

function Get-WebPage([string]$url)
{   
    $bypassonlocal = $false
    $proxyuri = "http://" + $env:COMPUTERNAME
    $proxy = New-Object system.Net.WebProxy($proxyuri, $bypassonlocal)
    $wc = new-object net.webclient
    $wc.credentials = [System.Net.CredentialCache]::DefaultCredentials
    $wc.proxy = $proxy
    $pageContents = $wc.DownloadString($url)
    #write-host $wc.ResponseHeaders.Get("WFE")
    $wc.Dispose()
    return $pageContents
}

#Central Administration
write-host "Warming up Central Administration..."
$WebApps = Get-SPWebApplication -IncludeCentralAdministration
Get-SPWebApplication -IncludeCentralAdministration | ? {$_.IsAdministrationWebApplication -eq $true} | % { $Req = Get-WebPage $_.url }
write-host "`nCentral Administration Warmed up!"

# Warm up Host Name Site Collections (HNSC)
 Write-Host "Warming up Host Name Site Collections (HNSC)...`n"
 $hnsc = Get-SPSite -Limit All |? {$_.HostHeaderIsSiteName -eq $true} | Select Url
 foreach ($sc in $hnsc) {
        write-host "Processing HNSC: "$sc.Url
  $Req = Get-WebPage $sc.url  
 }

# Clean Temporary Files
 Remove-item "$env:systemroot\system32\config\systemprofile\appdata\local\microsoft\Windows\temporary internet files\content.ie5\*.*" -Recurse -ErrorAction SilentlyContinue
 Remove-item "$env:systemroot\syswow64\config\systemprofile\appdata\local\microsoft\Windows\temporary internet files\content.ie5\*.*" -Recurse -ErrorAction SilentlyContinue

stop-transcript

When and How often we’ve to run Warm up Scripts?
Usually, We schedule it to run before the beginning of the day. It’s a good idea to schedule the PowerShell script via Task scheduler and Warmup script must be scheduled on all WFEs of your SharePoint Farm.

Here is how you can schedule PowerShell scripts using Windows task scheduler: Create a Scheduled Task for PowerShell Script with Windows Task Scheduler

Use “Search Crawl Account”to run warm-up script (Provided, This account has “Login as a Batch Job rights!”).

You can create a scheduled task with command line:
schtasks /create /tn “Warmup Script” /ru <<Domain\Account>> /rp <<Password>> /rl highest /sc daily /st 01:00 /ri 60 /du 24:00 /tr “PowerShell.exe -ExecutionPolicy Bypass D:\Scripts\Warmup.ps1”  

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

5 thoughts on “SharePoint 2013 Warmup Scripts using PowerShell

  • March 27, 2018 at 12:02 PM

    This comment has been removed by the author.

    Reply
  • March 8, 2018 at 3:42 PM

    Hi,
    Wondering with this Warm Up Script which I required for SharePoint 2013 which was recently configured ADFS, now we need to implement Warm Up Script. Could you help on this how we can proceed with both Get and Post method.
    Need to authenticate via Cookie where we have credentials not through passing direct credentials.
    Thanks,
    Vijaivel S

    Reply
    • March 10, 2018 at 5:16 PM

      Well, in IIS 8 or later, we’ve “Application Initialization” module to replace warmup scripts. Install this module and set Application Pool’s Start Mode to “AlwaysRunning”. I’ll publish an article soon.

      Reply
    • March 27, 2018 at 12:13 PM

      Hi, Apologize…Instead of reply I have added comment. Please have a look and waiting for your reply!!!
      Is it possible to have warm up script which can authenticate ADFS using the cookie value(Contains Credentials) & Invoke-Webrequest
      Thanks
      Vijaivel S

      Reply
  • August 7, 2016 at 3:27 AM

    Thanks for the link back to http://spbestwarmup.codeplex.com/

    Glad you like it! If you have any suggestions to improve please drop a line. -SPJeff

    Reply

Leave a Reply