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 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.
        $path = $dir + "\" + $ + "\Web.config"
        echo $path
        $backup = $path + "_$currentDate.bak"
        $xml = New-Object XML
        $element = $xml.configuration.SharePoint.BlobCache
        echo $element
        $element.location = "X:\BlobCache\14"
        $element.enabled = "true"
        $element.maxSize = "5"
        echo $element

Warning: count(): Parameter must be an array or an object that implements Countable in /homepages/35/d274356660/htdocs/basementjack/wp-includes/class-wp-comment-query.php on line 405

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:

    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.