Enable/Disable Custom Error by editing Web.Config using PowerShell

To reveal actual errors in SharePoint, we used to turn off custom error page by editing web.config file, located in the root of SharePoint virtual directory. sharepoint custom error page web.config

  • Open the web.config in Notepad
  • Search for “CallStack”, Change it from “false” to “true”
  • Search for “CustomErrors mode”, Change its value from “On” to “Off”
  • Save and Close.
Warning: Web.config file is highly sensitive! even a small typo may result your SharePoint site Crash!!

Typical SharePoint 2007 “Unknown Error”

sharepoint custom error web.config

and SharePoint 2010’s “An unexpected error has occurred”
 sharepoint 2010 web.config customerrors
Alright, Wouldn’t it be a good idea to make these changes by PowerShell Scripts? Sure, Here is the PowerShell script to enable or disable SharePoint custom error page and reveal actual error by editing Web.Config file:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function ConfigureCustomError()
  Param( [Parameter(Mandatory=$true)] [string]$WebAppURL,  [parameter(Mandatory=$true)] $Option )

 #Get the web application
 $WebApp = Get-SPWebApplication $WebAppURL
 #Get the Web Application Zone
 $Zone = $WebApp.AlternateUrls[0].UrlZone
 # Get the IIS settings for the zone
 $IISSettings = $WebApp.IISSettings[$zone]
 # Get the path from the settings
 $WebConfigPath = $IISSettings.Path.ToString() + "\web.config"

 #Backup web.config file
 $now = [datetime]::now.ToString('dd-MM-yyyy hh-mm-ss')
 $BackupFile =$IISSettings.Path.ToString()+ "\web.config.$now"
 copy-item $WebConfigPath -destination $BackupFile

 #Get the Web.config File
 $XMLDoc = new-object System.Xml.XmlDocument
  "OFF" { 
    #Apply Change
    $XMLDoc.get_DocumentElement()."sharepoint".safemode.callstack = "True"
    $XMLDoc.get_DocumentElement()."system.web".customErrors.mode = "Off"
    Write-Host "Custom Error Mode has been Disabled, Now you can get the actual errors!"
  "ON" {
    $XMLDoc.get_DocumentElement()."sharepoint".safemode.callstack = "False"
    $XMLDoc.get_DocumentElement()."system.web".customErrors.mode = "ON"
    Write-Host "Custom Error Mode has been Enabled, SharePoint displays default custom error Page!"

#Call the function to Turn OFF custom errors
ConfigureCustomError "https://sharepoint.crescent.com" "OFF"

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *