{"id":17121,"date":"2010-09-14T00:01:00","date_gmt":"2010-09-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/14\/use-powershell-to-script-changes-to-the-sharepoint-web-config-file\/"},"modified":"2010-09-14T00:01:00","modified_gmt":"2010-09-14T00:01:00","slug":"use-powershell-to-script-changes-to-the-sharepoint-web-config-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-script-changes-to-the-sharepoint-web-config-file\/","title":{"rendered":"Use PowerShell to Script Changes to the SharePoint Web.config File"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: Learn how to use Windows PowerShell to script changes to the <strong>web.config<\/strong> file on SharePoint 2010.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/> Hey, Scripting Guy! I need help using Windows PowerShell to automate making changes to the <strong>web.config<\/strong> file on my SharePoint 2010 farm. Do you know of a way to automate making these changes?<\/p>\n<p>&#8212; JG<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/> Hello JG, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. We are continuing with Guest Blogger Week, and today&rsquo;s guest blogger is Josh Gavant who will talk about SharePoint.<\/p>\n<p>Josh is a Premier Field Engineer (PFE) with Microsoft who specializes in both SharePoint and Windows PowerShell. Before joining Microsoft in fall 2009 he helped build and maintain an enterprise SharePoint farm for a United States government agency. Find SharePoint and Windows PowerShell-related discussions from Josh and contact him via his blog at <a href=\"http:\/\/blogs.msdn.com\/b\/besidethepoint\">http:\/\/blogs.msdn.com\/b\/besidethepoint<\/a>.<\/p>\n<p>Take it away, Josh. <\/p>\n<p>&nbsp;<\/p>\n<h4>What SharePoint Is and Is Not<\/h4>\n<p>When discussing SharePoint with customers, I often find myself emphasizing that a SharePoint farm is, in essence, a logical collection of services&mdash;not a collection of physical servers. When administrators and developers make modifications to their farm, they are manipulating and configuring these logical services; changes to files and other &ldquo;physical&rdquo; artifacts are a side effect of the logical changes. Another way of expressing this is to emphasize that changes to SharePoint should actually be perceived only as changes to the Object Model and ultimately the shared databases, not to any server-level constructs (for example, the file system or registry).<\/p>\n<p>To this end, SharePoint engineers constantly emphasize to developers the importance of using Solution (capital S) packages to deploy customizations to SharePoint. Solution packages upload all the files and other pieces necessary for new SharePoint artifacts to the SharePoint database, from where SharePoint itself handles deploying them to component servers as necessary.<\/p>\n<p>The Solution package is well and good for developers, but what about administrators who need to make farm-wide changes? For example, administrators sometimes need to modify the <strong>web.config<\/strong> files that govern the behavior of SharePoint Web Applications, or need to modify IIS settings, registry keys, or files on multiple SharePoint servers. Ideally, administrators would create single &ldquo;solutions&rdquo; to deal with configuring a single SharePoint logical service, instead of having to repeat processes on lots of physical servers. The SharePoint administrator tools are fine for single server installations, as shown in the following image. But such a procedure is difficult to maintain, hinders scale-out of a SharePoint farm, and is antithetical to the management paradigm espoused above, viewing SharePoint as a collection of logical services.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5141.HSG9141001_04ECB3C5.jpg\"><img decoding=\"async\" height=\"394\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3021.HSG9141001_thumb_2DC7A301.jpg\" alt=\"Image of SharePoint 2010 Central Administration\" border=\"0\" title=\"Image of SharePoint 2010 Central Administration\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>So what is an administrator to do? Windows PowerShell scripts, the SharePoint Management Shell, and the SharePoint administrative object model provide a <strong>Solution<\/strong> solution (sic) for SharePoint farm administrators. In this post, we explore using these tools to write a single reusable script that modifies the <strong>web.config<\/strong> file for a Web Application across an entire farm. Though we&rsquo;ll focus on a single example, the concepts can be utilized for any <strong>web.config<\/strong> modifications.<\/p>\n<h4>Introducing Today&rsquo;s Audience Participant, the SharePoint BlobCache<\/h4>\n<p>A commonly implemented configuration change for SharePoint Publishing sites is that of enabling the <strong>BlobCache<\/strong> for the parent Web Application. The <strong>BlobCache<\/strong> is a special file-system cache created on SharePoint front-end web servers to store binary large objects (BLOBs) such as images, text files, and media files retrieved from SharePoint content databases. By storing these files in the <strong>BlobCache<\/strong>, the front-end server saves a trip back to the content database when serving these files for subsequent requests, improving response times. In addition, when administrators configure and enable the <strong>BlobCache<\/strong>, they can cause SharePoint to add a HTTP cache header to the file as it is delivered to the browser, which instructs the browser to rely on its cached version of the file for some defined period. This helps speed up repeat requests for pages on the client side.<\/p>\n<p>With these benefits, it&rsquo;s easy to see why administrators and managers would want to turn on the SharePoint <strong>BlobCache<\/strong>. Unfortunately, the documented way to do so involves manually changing the <strong>web.config<\/strong> file for every effected Web Application on every front-end Web server, a process that can be difficult to maintain and control without errors. What we need is a way to modify the <strong>web.config<\/strong> for a Web Application across the farm in a single stroke.<\/p>\n<h4>SPWebConfigModification<\/h4>\n<p>Enter <strong>SPWebConfigModification<\/strong>. This object contains properties for making changes to <strong>web.config<\/strong> files for an associated Web Application. The job of the administrator or developer is to construct one of these objects and assign appropriate values to its properties, and then add the new object to the collection of modifications associated with a given Web Application. After this is done, SharePoint takes care of applying the modification to all instances of that Web Application across the farm, including any additional servers added in the future. Another benefit is that by removing the modification from the list, the change is automatically reverted, which returns every server to its default original state.<\/p>\n<p>Voila! With <strong>SPWebConfigModification<\/strong>, <strong>web.config<\/strong> changes no longer need to involve directly touching files or settings on any server.<\/p>\n<p>So how can you create and apply these changes via Windows PowerShell? There are three steps to follow: <\/p>\n<ol>\n<li>Construct and configure the <strong>SPWebConfigModification<\/strong> object.<\/li>\n<li>Get a <strong>SPWebApplication<\/strong> object to which to add your modifications.<\/li>\n<li>Wrap your work in a function for reusability. <\/li>\n<\/ol>\n<p>You&rsquo;ll also implement a complementary function to potentially undo the changes that you make. Let&rsquo;s get started.<\/p>\n<h4>Construct and Configure the SPWebConfigModification Object<\/h4>\n<p>When Windows PowerShell and its loaded snap-ins do not contain a cmdlet for returning an object that you want to use, you often can use the <strong>New-Object<\/strong> cmdlet to create an instance of that object. <strong>New-Object<\/strong> takes the fully qualified name of a type and an array of objects as its two main parameters; the array of objects contains the arguments to be passed to the object&rsquo;s <strong>constructor<\/strong> method.<\/p>\n<p>In this case, you are going to call the default constructor for <strong>Microsoft.SharePoint.Administration.SPWebConfigModification<\/strong>, which takes no arguments, and then store your newly constructed object in a variable for further manipulation. Here is the line:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft.SharePoint.Administration.SPWebConfigModification<\/span> <\/div>\n<\/blockquote>\n<p>Next, set the properties needed to change the enabled attribute for the <strong>BlobCache<\/strong> node in the <strong>web.config<\/strong> file:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;configuration\/SharePoint\/BlobCache&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;enabled&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;true&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Sequence<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Owner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;BlobCacheMod&#8221;       <br \/><\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureChildNode<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureAttribute<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureSection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #0000ff\">Type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span> <\/div>\n<\/blockquote>\n<p>The various properties can be tricky, and the required values change based on the <strong>SPWebConfigModificationType<\/strong> chosen for the <strong>Type<\/strong> property. In this case, you want to make sure that an attribute on an existing node takes on a specific value, so we choose <strong>EnsureAttribute<\/strong> as the value for the <strong>Type<\/strong> property. There are a few ways of choosing values from an <strong>Enum<\/strong> in Windows PowerShell; for this script, I am simply using the integer value that the specific <strong>Enum<\/strong> choice corresponds to, as noted in the comments for setting the <strong>Type<\/strong> property.<\/p>\n<p>The <strong>Path<\/strong> property expects an XPath expression pointing to the element that we will be considering; <strong>Name<\/strong> refers to the attribute on that element to check. <strong>Sequence<\/strong> determines precedence if multiple <strong>SPWebConfigModification<\/strong> objects attempt to modify the same attribute; the lower the number, the higher the priority. By choosing <strong>0<\/strong>, I&rsquo;ve overridden any other change. <strong>Value<\/strong> determines the actual value that the attribute will be set to. The result of this modification will be setting <strong>enabled=&rdquo;true&rdquo;<\/strong> on the <strong>BlobCache<\/strong> node in <strong>web.config<\/strong>.<\/p>\n<p>One last property is the <strong>Owner<\/strong> property. The value of this property is arbitrary, but it is useful in locating specific modifications for manipulation after they have already been added to a collection. As you will see, for our reversion function you can find the modifications by checking the value assigned to this property.<\/p>\n<p>With these steps completed, you have the first of our two <strong>SPWebConfigModifications<\/strong>. The second one sets the <strong>Max-Age<\/strong> attribute on the same <strong>BlobCache<\/strong> node. This sets the <strong>Max-Age<\/strong> HTTP header on the outgoing file so that browsers know to cache the object for the specified period. This modification is seen in the following Windows PowerShell code:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft.SharePoint.Administration.SPWebConfigModification<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;configuration\/SharePoint\/BlobCache&#8221;       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;max-age&#8221;       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;86400&#8221;       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Sequence<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Owner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;BlobCacheMod&#8221;       <\/p>\n<p><\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureChildNode<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureAttribute<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureSection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #0000ff\">Type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span> <\/div>\n<\/blockquote>\n<p>The setup and properties for this modification follow the same pattern as the previous one. Choose a value of 86400 seconds, or 24 hours, as a reasonable time for the browser to rely on its cached version of a file. Once again, set the <strong>Owner<\/strong> property to <strong>BlobCacheMod<\/strong> to help find this modification when you need to remove it.<\/p>\n<h4>Get a SPWebApplication Object with Which to Work<\/h4>\n<p>You have created your <strong>SPWebConfigModification<\/strong> objects; now add them to the <strong>WebConfigModifications<\/strong> property of the Web Application they will be applied to. To do this, start by creating an instance of a <strong>SPWebApplication<\/strong> object. In the SharePoint 2007 days, I might have asked the user of the function to supply us with a URL, and then I would use the static <strong>SPWebApplication.Lookup()<\/strong> method to return an instance. While this tactic would still be valid in SharePoint 2010, there are now more robust options available.<\/p>\n<h4>Introducing SPCmdletPipeBind&lt;TCmdletObject&gt;<\/h4>\n<p>As with many other Microsoft-provided foundations in SharePoint, the SharePoint team has taken the Windows PowerShell framework and extended it specifically for SharePoint: the classes and utilities provided by the <strong>Microsoft.SharePoint.PowerShell<\/strong> namespace. This namespace contains not only the 500-plus SharePoint-specific cmdlets, but also a set of utility classes for creating more SharePoint cmdlets and scripts, in addition to allowing us to work more easily with the SharePoint object model.<\/p>\n<p>In SharePoint 2010, instead of asking the user for a string or even an <strong>SPWebApplication<\/strong> object, you can specify a <strong>SPWebApplicationPipeBind<\/strong> object, itself derived from the generic <strong>SPCmdletPipeBind&lt;TCmdletObject&gt;<\/strong> class. The idea behind the <strong>PipeBind<\/strong> classes is that they allow the same object to be referred to in different ways. In the case of <strong>SPWebApplication<\/strong>, you can pass a string representing the URL of the Web Application, a GUID identifying the specific SharePoint configuration object that is the Web Application, or even an actual <strong>SPWebApplication<\/strong> object (that is, output from the pipeline or stored in a variable). The <strong>SPWebApplicationPipeBind<\/strong> class takes care of ultimately returning a <strong>SPWebApplication<\/strong> object for further manipulation in the shell. An immediate benefit of using the <strong>PipeBind<\/strong> classes is you can accept <strong>SPWebApplication<\/strong> objects output by previous cmdlets in the pipeline without making it difficult to call the cmdlet in standalone fashion, passing it a URL as a string.<\/p>\n<p>The function will expect a <strong>SPWebApplicationPipeBind<\/strong> object and will convert this object into a true <strong>SPWebApplication<\/strong> object for further manipulation. Here are the relevant lines in the script:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">param(<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">[Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ValueFromPipeline<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Position<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">)]<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApplication<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$WebApplication<\/span><span style=\"color: #000000\">.Read()<\/span> <\/div>\n<\/blockquote>\n<p>The <strong>Parameter<\/strong> attribute is typical for an advanced Windows PowerShell function, specifying that the parameter is mandatory, can be accepted directly from the pipeline, and does not have to be named. The second pair of square brackets (<strong>[ ]<\/strong>) specify the type of the parameter: a <strong>SPWebApplicationPipeBind<\/strong> object. Finally, the first action within the <strong>Process<\/strong> block of the cmdlet is to call the <strong>Read<\/strong> method on the <strong>PipeBind<\/strong> object that was received, which is the defined method for extracting the relevant <strong>TCmdletObject<\/strong> from the <strong>SPCmdletPipeBind<\/strong>-derived class.<\/p>\n<p>What this all means at the end of the day is that the <strong>$WebApp<\/strong> variable now contains a <strong>SPWebApplication <\/strong>object to which you can add your <strong>web.conf<\/strong>ig modifications.<\/p>\n<h4>Add the Modifications to the SPWebApplication Object<\/h4>\n<p>While the previous section might have been your first direct encounter of the <strong>PipeBind<\/strong> kind, this next part should be a bit simpler. The <strong>SPWebApplication<\/strong> instance you are now the proud parent of contains a property <strong>WebConfigModifications<\/strong> that references a <strong>Collection<\/strong> <strong>&lt;SPWebConfigModification&gt;<\/strong> object; that is, a collection of <strong>SPWebConfigModifications<\/strong>. The <strong>Collection<\/strong> class offers a lot of useful functionality, but let&rsquo;s focus only on its <strong>Add<\/strong> and <strong>Remove<\/strong> methods. To add the new <strong>SPWebConfigModification<\/strong> objects to the collection associated with a Web Application, use the following two lines:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.WebConfigModifications.Add(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.WebConfigModifications.Add(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<\/blockquote>\n<p>To ensure that the changes are persisted back to the SharePoint configuration object for the Web Application in the configuration database and thus propagated out to all instances of the Web Application in the farm, call <strong>Update()<\/strong>:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.Update()<\/span> <\/div>\n<\/blockquote>\n<p>Finally, to apply these new modifications across the farm, you can call the appropriate method on the <strong>SPWebService<\/strong> object, the host service for all SPWe<strong>b<\/strong>Applications, which is also the parent of the Web Application in question:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.Parent.ApplyWebConfigModifications()<\/span> <\/div>\n<\/blockquote>\n<p>And with that, the <strong>web.config<\/strong> files for this Web Application across our entire farm will be modified. If you want to verify these changes, call <strong>Get-SPWebApplication<\/strong> and check the value of the <strong>WebConfigModifications<\/strong> property for the Web Application that you modified.<\/p>\n<h4>Complete Function<\/h4>\n<p>Here is the complete script for the previous work, all wrapped in a nice function.<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Enable-SPBlobCache<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">param(<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">[Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ValueFromPipeline<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Position<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">)]<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApplication<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$WebApplication<\/span><span style=\"color: #000000\">.Read()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModification<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">enable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">BlobCache<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft.SharePoint.Administration.SPWebConfigModification<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;configuration\/SharePoint\/BlobCache&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;enabled&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Sequence<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Owner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;BlobCacheMod&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureChildNode<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureAttribute<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureSection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #0000ff\">Type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #000000\">.Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;true&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModification<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">enable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">client-side<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Blob<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">caching<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(max-age)<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft.SharePoint.Administration.SPWebConfigModification<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;configuration\/SharePoint\/BlobCache&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;max-age&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Sequence<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Owner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;BlobCacheMod&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureChildNode<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureAttribute<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">##<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">SPWebConfigModificationType.EnsureSection<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #0000ff\">Type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #000000\">.Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;86400&#8221;       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Add<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">mods,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">update,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">and<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">apply<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.WebConfigModifications.Add(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$configMod1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.WebConfigModifications.Add(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$configMod2<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.Update()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.Parent.ApplyWebConfigModifications()<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<\/blockquote>\n<h4>Reverting the Changes<\/h4>\n<p>Though creating a function to automate <strong>web.config<\/strong> changes across the farm is great, what happens if you need to revert your changes? Here is another situation where the utility of <strong>SPWebConfigModification<\/strong> shines. Remember that in our previous example, we set the <strong>Owner<\/strong> property of our modifications to <strong>BlobCacheMod<\/strong>. Removing the changes now becomes a matter of once again getting a reference to the <strong>SPWebApplication<\/strong> and its <strong>WebConfigModifications<\/strong> property, sifting through the collection and finding objects with the <strong>Owner<\/strong> property set to <strong>BlobCacheMod<\/strong>, and then calling the <strong>Remove<\/strong> method on the collection for each of these. You then wrap up by calling the <strong>Update<\/strong> method on the <strong>Web Application<\/strong> object and <strong>ApplyWebConfigModifications()<\/strong> on the <strong>SPWebService<\/strong> parent object to apply the changes. Here is the complete function:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Disable-SPBlobCache<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">param(<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">[Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ValueFromPipeline<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #2b91af\">$true<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Position<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">)]<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApplication<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$WebApplication<\/span><span style=\"color: #000000\">.Read()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$mods<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">@()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$mod<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.WebConfigModifications<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$mod<\/span><span style=\"color: #000000\">.Owner<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\"> &#8220;BlobCacheMod&#8221; <\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$mods<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$mod<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$mod<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$mods<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">[void]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.WebConfigModifications.Remove(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$mod<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.Update()<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp;&nbsp;&nbsp; <\/span><span style=\"color: #2b91af\">$WebApp<\/span><span style=\"color: #000000\">.Parent.ApplyWebConfigModifications()<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">}<\/span> <\/div>\n<\/blockquote>\n<p>So there it is. Two functions, <strong>Enable-SPBlobCache<\/strong> and <strong>Disable-SPBlobCache<\/strong>, which take a <strong>Web Application<\/strong> object from the pipeline or via command-line input and make the necessary <strong>web.config<\/strong> changes to enable or disable the <strong>BlobCache<\/strong> across the farm. Other changes to <strong>web.config<\/strong> files can be automated in the same way.<\/p>\n<p>I typically wrap these and other custom cmdlets into a module and import the module into my SharePoint Management Shell when I need the functions. You could also create a script and dot-source it, or create a script that defines the functions and then calls them. The choice is yours.<\/p>\n<p>There are many other opportunities in Windows PowerShell to simplify and unify cross-farm and cross-component administration in a SharePoint farm. Stay tuned for future posts.<\/p>\n<p>&nbsp;<\/p>\n<p>JG, that is all there is to using Windows PowerShell to automate changes to the SharePoint 2010 <strong>web.config<\/strong> file. Guest Blogger Week will continue tomorrow when Glyn Clough will talk more about SharePoint.<\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong><\/strong><\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Learn how to use Windows PowerShell to script changes to the web.config file on SharePoint 2010. &nbsp; Hey, Scripting Guy! I need help using Windows PowerShell to automate making changes to the web.config file on my SharePoint 2010 farm. Do you know of a way to automate making these changes? &#8212; JG &nbsp; [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,187,3,59,45],"class_list":["post-17121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-josh-gavant","tag-scripting-guy","tag-sharepoint","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Learn how to use Windows PowerShell to script changes to the web.config file on SharePoint 2010. &nbsp; Hey, Scripting Guy! I need help using Windows PowerShell to automate making changes to the web.config file on my SharePoint 2010 farm. Do you know of a way to automate making these changes? &#8212; JG &nbsp; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}