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>
  2. Click on Settings from the left navigation >> Click on “Classic settings page” (https://<tenant> >> 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” 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
#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:

$TenantAdminURL= ""
$SiteURL= ""

#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 = ""
#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?

$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")
        Set-PnPTenantSite -Identity $_.URL -DenyAddAndCustomizePages:$false
        Write-host "`tCustom Script has been Enabled!" -f Green
        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. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

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

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

  • 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

  • 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.

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

      • 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:

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

        Many thanks,

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

    • This is because, the Latest PnP.PowerShell module removed the “Update” method. So, use:
      Set-PnPTenantSite -Identity $SiteURL -DenyAddAndCustomizePages:$false

  • 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

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