How to Enable Custom Script in SharePoint Online Using PnP PowerShell?

Do you want to enable the custom script in SharePoint Online? In this tutorial, I will show you how to enable custom script in SharePoint Online using PnP PowerShell.

Why Enable Custom Script in SharePoint Online?

Let me show you, first of all, why we need to enable custom script in SharePoint Online.

In SharePoint Online modern sites, custom script is disabled by default because custom scripts can execute code with elevated permissions. Below are the things that are not possible when the custom script is disabled.

  • The script editor web part and content editor web part are not available on the SharePoint Online site.
  • Save list as template option will be disabled in the SharePoint Online list (Option will be hidden from the SharePoint list settings page)
  • Files containing scripts will be disabled from uploading to the SharePoint site, etc.

To enable the custom script in a SharePoint Online site, you need to enable it in both places.

  • Enable at the SharePoint Online admin center
  • Enable at the SharePoint Online site level

Enable Custom Script in SharePoint Online Admin Center

Follow the below steps to enable custom scripts in the SharePoint Online admin center.

  1. Open the classic SharePoint Online tenant settings page (https://<tenantname>-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx)
  2. Alternatively, you can open the SharePoint Online admin center (E.g., https://szg52.sharepoint.com/), navigate to Settings, and click on the “classic settings page” link.
  3. Here, navigate to the “Custom Script” section and choose the “Allow users to run custom script on self-service created sites” radio button like in the screenshot below:
Enable Custom Script in SharePoint Online Admin Center

Once this is done, you can enable the custom scripts at the SharePoint site level.

Read Get All Lists and Libraries from SharePoint Online Site using PnP PowerShell

Enable Custom Script in SharePoint Online Site using Browser

Now, follow the below steps to enable the custom script in the SharePoint Online site using the browser.

  • Open the SharePoint Online admin center, then click on Sites -> “Active sites” from the left navigation. This will display all the SharePoint online sites.
  • Select the particular SharePoint Online site for which you want to enable custom scripts, click on Settings, and then click on Edit in the Custom Scripts section like below:
Enable Custom Script in SharePoint Online Site
  • Then, on the “Custom scripts” page, choose “Allowed” in the “Custom scripts setting” like the screenshot below.
Enable Custom Script in SharePoint Online Site using Browser

This is how to enable custom scripts in a SharePoint Online modern site from the browser itself.

Check out Delete and Recover a SharePoint Online Site Using PowerShell and PnP PowerShell

Enable Custom Script in SharePoint Online Site using PnP PowerShell

I will show you now how to enable custom script in SharePoint Online site using PnP PowerShell.

Below is the complete PowerShell script.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Enable Custom Script on the specified site
Set-PnPTenantSite -Identity $siteUrl -DenyAddAndCustomizePages:$false

# Verify Custom Script setting
$site = Get-PnPTenantSite -Identity $siteUrl
$site.DenyAddAndCustomizePages

To enable custom scripts on a specific site collection, you can use the Set-PnPTenantSite PnP PowerShell cmdlet. Here, you need to pass the parameter below:

-DenyAddAndCustomizePages:$false

After enabling custom scripts, you can verify that the setting has been applied correctly by running the following command:

$site = Get-PnPTenantSite -Identity $siteUrl
$site.DenyAddAndCustomizePages

I executed the above script using VS code, and you can see the output in the screenshot below:

Enable Custom Script in SharePoint Online Site using PnP PowerShell

This is how to enable the custom script in a SharePoint Online site using PnP PowerShell.

Here is another script you can also write using PnP PowerShell to enable custom scripts using PnP PowerShell.

Connect-PnPOnline -Url "https://szg52.sharepoint.com/sites/PowerPlatformFAQs" -Interactive

# Enable Custom Script on the specified site
Set-PnPTenantSite -Identity $siteUrl -NoScriptSite:$false

# Verify Custom Script setting
$site = Get-PnPTenantSite -Identity $siteUrl
$site.NoScriptSite

Both methods should work fine.

In this tutorial, I have explained how to enable custom scripts for a SharePoint Online site using PnP PowerShell. Do let me know in the comments below if you face any issues while executing the script.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.