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

6 Answers 6


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.


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.

  • I tried that too but still the result is same....! May 12, 2010 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 ?

  • The script also runs for me using localhost as the url on SharePoint 2010.
    – Steve P
    Oct 13, 2010 at 18:35

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



.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.

  • Powershell can coerce strings to enums.
    – Steve P
    Oct 17, 2010 at 8:28

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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