How to Enable Custom Script in SharePoint Online using PowerShell?

Requirement: Allow custom script in SharePoint Online!

By default, Custom scripts are disabled in SharePoint Online modern sites for security reasons. So, the below features are blocked when the custom scripting is disabled:

  • Many web parts including content editor, script editor, etc are disabled.
  • Uploading files that potentially include scripts are blocked
  • SharePoint Designer script changes not allowed
  • Save Site as Template, Save document library as template links disabled
  • Solution Gallery, Sandbox Solutions, Theme Gallery, HTML Field Settings are not available
  • All other script-related featured are turned off

So, To fix the above problems, we need to enable custom script settings. Here is how to enable custom script in SharePoint Online:

How to Enable Custom Script in SharePoint Online through Admin Center?

To allow custom script in SharePoint Online, follow these steps:

  • Sign in to Office 365 and go to the SharePoint Online Admin Center: https://<tenant>
  • Click on Settings from the left navigation >> Scroll down to “Custom Script” section
  • Under Custom Script section, Set “Allow users to run custom script on personal site” and “Allow
    users to run custom script on self-service created sites” options. 
    enable custom script sharepoint online powershell
  • Click on “OK” to save your changes.
In Modern Admin center, This configuration is moved to “Classic Settings Page”! Click on Settings >> Click on the “Classic Settings page” link from the bottom of the page!

This enables custom scripting in the SharePoint Online tenant. However, this change may take up to 24 hours to reflect. To enable scripting on a particular site collection immediately, use this PowerShell script through SharePoint Online Management Shell.

Also, when you enable custom scripting through the web interface, you enable it for all sites in the tenant. However, you can enable or disable custom scripting on a specific site collection through PowerShell.

Enable Custom Script in SharePoint Online using PowerShell

Here is how to enable custom script in SharePoint Online using PowerShell at the site collection level.

#Config Parameters
#Get Credentials to connect
$Cred = Get-Credential
#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential $Cred
#sharepoint online enable custom scripts powershell - Disable DenyAddAndCustomizePages Flag
Set-SPOSite $SiteURL -DenyAddAndCustomizePages $False

To Disable Custom Script again, use:

Set-SPOsite $SiteURL -DenyAddAndCustomizePages $True

PnP PowerShell to Enable Custom Scripting in SharePoint Online

To enable custom script for a site collection, We’ve to set the “DenyAddandCustomizepages” flag using PowerShell in SharePoint Online:

$TenantAdminURL= ""
$SiteURL= ""
#Connect to Tenant Admin
Connect-PnPOnline $TenantAdminURL -Interactive

#Get the Tenant Site Object
$Site = Get-PnPTenantSite -Url $SiteURL
#Enable Custom Scripting by turning OFF Deny Flag
$Site.DenyAddAndCustomizePages = "Disabled"

Here is how to enable custom script in SharePoint online using PnP PowerShell Set-PnPSite cmdlet:

#Set Variables
$SiteURL = ""
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#sharepoint online allow custom scripts powershell 
Set-PnPSite -Identity $SiteURL -NoScriptSite $false

How about enabling custom script for All SharePoint Online Sites in the tenant immediately?

$TenantURL =  ""
#Get Credentials to connect
$Credential = Get-Credential
#Frame Tenant Admin URL from Tenant URL
$TenantAdminURL = $TenantURL.Insert($TenantURL.IndexOf("."),"-admin")

#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential
#Get All Site collections - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL' -and Url -notlike 'portals/hub'"

#Iterate through all sites
$Sites | ForEach-Object {
    Write-host "Processing Site Collection:"$_.URL

    #Enable Custom Script
    If ($_.DenyAddAndCustomizePages -ne "Disabled")
        $_.DenyAddAndCustomizePages = "Disabled"
        $_.Update() | Out-Null
        Write-host "`tCustom Script has been Enabled!" -f Green
        Write-host "`tCustom Script is Already Enabled!" -f Yellow

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

5 thoughts on “How to Enable Custom Script in SharePoint Online using PowerShell?

  • I believe you have to set flag as 0 or 1?

  • I cannot find Custom Script section

    • You can get in through classic admin center:


Leave a Reply