Configure Web.Config Custom Errors using PowerShell

It seems like every time I go to disable custom errors for the purposes of troubleshooting, I have to turn to the interwebs to remember the exact settings I need to change and what I need to change them to. Of course it’s all done in the web.config file for the web application with which you are working – and it’s really just two settings that need tweaked…

Instead of doing this manually, I thought “What if I could create two PowerShell Functions to manipulate these values?”

Well, lucky for me I had already done this to enable and disable SP Blob Caching – so it was pretty easy to modify that code to change the customErrors and CallStack settings in the web.config.

Essentially the hardest part is making sure you grab the right configuration path from the web.config, once you’ve got that, it’s just a matter of creating a new object (Microsoft.SharePoint.Administration.SPWebConfigModification) and then setting values of that object.

Once you’ve created your 2 $configMod objects, you call the $webapp.WebConfigModifications.Add method for each mod – and then call the Update() method to apply. Hit your page again and you should see an uglier, yet more informational error message.

Once you have dot-sourced the function, here is how you disable the SharePoint (custom) Errors:

Disable-SPCustomErrors -WebApplication http://webappurl

And here is how you re-enable the SharePoint (custom) Errors:

Enable-SPCustomErrors -WebApplication http://webappurl

And without further ado, here is the code containing two functions – Enable-SPCustomErrors and Disable-SPCustomErrors: