SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to update the web.config file through Powershell using the SharePoint class "SPWebConfigModification" however facing the issue mentioned below, please help me to fix this.

$url = New-Object System.Uri("http://serverName:port/")
$webApp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($url) 
$mod = New-Object -TypeName "Microsoft.SharePoint.Administration.SPWebConfigModification" 
$mod.Path = "configuration/system.web/customErrors"
$mod.Name = "mode"
$mod.Sequence = 0
$mod.Owner = "powershell" 
$mod.Type = "EnsureAttribute"
$mod.Value = "Off"


Exception calling "ApplyWebConfigModifications" with "0" argument(s): "'' is an invalid expression." At line:1 char:47 + $webApp.WebService.ApplyWebConfigModifications <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

share|improve this question
up vote 2 down vote accepted

Is this a new Web Application?

Often when dealing with SPWebConfigModification the ApplyWebConfigModifications() call will fail because of an earlier broken SPWebConfigModification that is sitting in the collection.

Try the following to see if there's anything unexpected in there

$webApp.WebConfigModifications | Format-Table Name,Owner

if the only things in there are yours, try $webApp.WebConfigModifications.Clear() first, otherwise you can clear it out by hand.

share|improve this answer

I found this article where the author is doing the same thing more or less, however he calls Update() BEFORE calling ApplyWebConfigModifications(). You might want to try that.

share|improve this answer
I tried that too but still the result is same....! – Karthikeyan May 12 '10 at 23:42

I have copied your script I run a test and seems to be working fine (on a web app with port number and also on another without)

Are you running this from the server where front end http://serverName:port/ runs ?

share|improve this answer
The script also runs for me using localhost as the url on SharePoint 2010. – Steve P Oct 13 '10 at 18:35

It might help to get a more detailed stacktrace of the exception.


share|improve this answer

.Type is looking for an Enum, not the string "EnsureAttribute"
The easiest way I've found to handle this is use Visual Studio to determine the value of the enum you're looking for. EnsureAttribute is 1, so change

$mod.Type = "EnsureAttribute" to be
$mod.Type = 1

and you should be good to go.

share|improve this answer
Powershell can coerce strings to enums. – Steve P Oct 17 '10 at 8:28

This worked for me $webApp.WebConfigModifications.Clear()

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.