Simple PowerShell to Enable BLOB Cache on multiple SharePoint sites.

I needed to enable / configure BLOB caching on multiple sharepoint sites.

This is done by editing the web.config of each SharePoint site, on each SharePoint server.

I wrote this down and dirty script so I would not need to edit all the web.config’s by hand (I had about 20 web.configs to touch)

Note that since its just editing the web.config, we don’t need to run this in a SharePoint shell – I ran it from an ordinary PowerShell command prompt on my workstation.

The script:

 
Echo "Run this script under an admin account with rights to the servers being hit"
 
$dir = "\\Server\c$\inetpub\wwwroot\wss\VirtualDirectories"
$currentDate = (get-date).tostring("mm_dd_yyyy-hh_mm_ss")
# loop through each subdirectory to find each sharepoint site.
foreach ($subdir in dir $dir)
{
   #  Here In my case, all my SharePoint sites had mydomain.com as part of the folder names,
   #  So the contains statement was an easy way to only touch the web.config's of actual SharePoint sites
   #  while leaving alone central admin and other non-SharePoint websites IIS had.
 
   if($subdir.Name.Contains("mydomain.com"))
   {
        $path = $dir + "\" + $subdir.name + "\Web.config"
        echo $path
 
        $backup = $path + "_$currentDate.bak"
        $xml = New-Object XML
        $xml.Load($path)
        $xml.Save($backup)
        $element = $xml.configuration.SharePoint.BlobCache
        echo $element
        $element.location = "X:\BlobCache\14"
        $element.enabled = "true"
        $element.maxSize = "5"
        echo $element
        $xml.Save($path) 
   }
}

2 thoughts on “Simple PowerShell to Enable BLOB Cache on multiple SharePoint sites.”

  1. Remco, you’re absolutely right!

    I took a look at the script which attributes this page:

    http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/14/use-powershell-to-script-changes-to-the-sharepoint-web-config-file.aspx

    The article above goes into a lot of detail over the better way to do this, so I won’t rehash it here, but in summary, doing it as the article and Remco suggest is better because SharePoint manages the change to the web.config – this way if you ever add or replace a SharePoint server on your farm, the changes will be added to it automatically.

    Thanks Remco for commenting!
    (Note, I’ll leave up the original text, as it’s got some merit in Search results, and is a baseline example for editing XML with powershell)

Comments are closed.