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 Team sites, communication sites, Root site, sites users create themselves, and in OneDrive 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 are not allowed, and Custom actions no longer work. New Subsite and Delete Site take you to the Site Settings page in the browser.
  • Save Site as Template, list templates, Save document library as template links disabled in SharePoint sites
  • Solution Gallery, Sandbox Solutions, Theme Gallery, and HTML Field security are not available in the site settings
  • Any of the following file types are blocked by default: .asmx .ascx .aspx .htc .jar .master .swf .xap .xsf
  • All other script-related features are turned off

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

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

How do I enable custom scripting in SharePoint Online? To allow custom script in SharePoint Online, follow these steps:

  1. Sign in to Microsoft 365 through the browser and go to the SharePoint Admin Center: https://<tenant>-admin.sharepoint.com
  2. Click on Settings from the left navigation >> Click on “Classic settings page” (https://<tenant>-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx) >> Scroll down to “Custom Script” section
  3. Under the 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
  4. 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 settings and for any further self-service sites or sites created by Admins. 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.

Enabling custom script in SharePoint Online is a relatively simple process. When you enable custom scripting through the SharePoint Online Admin center, you enable it for all sites in the tenant. Whereas, you can enable or disable custom scripting on a specific SharePoint Online site collection through PowerShell.

Enable Custom Script in SharePoint Online using PowerShell

How to enable custom script in the SharePoint Online site? Let’s take a look at the steps for enabling custom script in SharePoint Online. The first thing you’ll need to do is open the SharePoint Online Management Shell and connect to your SharePoint Online tenant using the “Connect-SPOService -url https://yourtenant-admin.sharepoint.com” cmdlet. Once you’re connected, you’ll need to run the following cmdlet: Set-SPOsite -DenyAddAndCustomizePages 0

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

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"  
 
#Connect to SharePoint Online Tenant Admin portal
Connect-SPOService -URL $AdminSiteURL
  
#sharepoint online enable custom scripts powershell - Disable DenyAddAndCustomizePages Flag
Set-SPOSite $SiteURL -DenyAddAndCustomizePages $False

To disable the custom script for SharePoint Online site collection, use the below in the PowerShell console:

Set-SPOsite $SiteURL -DenyAddAndCustomizePages $True

PnP PowerShell to Enable Custom Scripting in SharePoint Online

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

#Parameters
$TenantAdminURL= "https://crescent-admin.sharepoint.com"
$SiteURL= "https://crescent.sharepoint.com/sites/Procurement"
 
#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"
$Site.Update()
$Site.Context.ExecuteQuery()

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

#Set Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/purchase"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

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

How to enable custom script for All Sites in SharePoint Online?

How about enabling the custom script for All SharePoint Online Sites in the tenant immediately, instead of doing it site by site basis?

#Parameters
$TenantURL =  "https://Crescent.sharepoint.com"
 
#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
        $_.Context.ExecuteQuery()
        Write-host "`tCustom Script has been Enabled!" -f Green
    }
    Else
    {
        Write-host "`tCustom Script is Already Enabled!" -f Yellow
    }
}

Reference: Allow or prevent custom script

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

  • I am trying to use the Set-PnPSite -Identity $SiteURL -NoScriptSite $false command but when later in my script I attempt to modify a property bag, I get the following error:

    Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Site might have NoScript enabled, this prevents setting some property bag values.

    Reply
    • This is because: Your Admin Center setting overwrites what you set through PowerShell! Make sure they are in sync.

      Reply
  • I get this error when I run this..
    Method invocation failed because [PnP.PowerShell.Commands.Model.SPOSite] does not contain a method named ‘Update’.

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

    Reply
  • I cannot find Custom Script section

    Reply
    • You can get in through classic admin center: https://YOURTENANT-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx

      Reply

Leave a Reply

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