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 the content editor, and script editor, 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 unavailable 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. This article will walk us through the steps to enable 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.

Allow 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 the 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

#Enable Custom Scripting by turning OFF Deny Flag
Set-PnPTenantSite -Identity $SiteURL -DenyAddAndCustomizePages:$false

You can also enable the 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")
    {
        Set-PnPTenantSite -Identity $_.URL -DenyAddAndCustomizePages:$false
        Write-host "`tCustom Script has been Enabled!" -f Green
    }
    Else
    {
        Write-host "`tCustom Script is Already Enabled!" -f Yellow
    }
}

In summary, we’ve shown how to enable Custom Script in SharePoint Online using SharePoint Admin center and PowerShell. By following these steps, you can easily enable Custom Script for a specific site collection or site, or for all site collections and sites in your SharePoint Online tenant. Microsoft Reference: Allow or prevent custom script

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!

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

  • I have a interactive video that I want to have on our MS Learning Pathways SharePoint Site. I’ve uploaded the file package with the .aspx and it doesn’t work. I’ve tried it on other SharePoint Sites (non-MS Learning Pathways created) and it works as expected, Do you know if there is something I need to do differently because the site was built from Learning Pathways? Custom Scripts is enabled.

    Reply
  • Is there a PowerShell script that would check to see if DenyAddAndCustomizePages is actually enabled or disabled on a site?

    Reply
  • Hello Salaudeen,

    I have a quick question. I am site collection admin for a site so May I enable custom script on the same site collection in any way or need SharePoint admin or global admin permission?

    Thank you

    Reply
  • 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
      • Hello Salaudeen,

        thank you very much for your awesome tutorial!

        I tried to follow it, and my static ASPX site is already working on some of my SharePoint sites. However, it still does not work on others. It would be great if you could take a look at the issue here:
        https://sharepoint.stackexchange.com/questions/306110/i-got-custom-scripts-enabled-but-my-static-aspx-web-page-doesnt-work-in-all

        Maybe you have a spontaneous idea what the problem could be?

        Many thanks,
        David

        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
    • This is because, the Latest PnP.PowerShell module removed the “Update” method. So, use:
      Set-PnPTenantSite -Identity $SiteURL -DenyAddAndCustomizePages:$false

      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 *