Skip to main content

How to enable custom scripts in SharePoint Online (using PowerShell)

BindTuning Team avatar
Written by BindTuning Team
Updated over 2 months ago

In SharePoint Online, you can enable and disable custom scripting at the site collection level. For modern UI sites, Microsoft has disabled custom scripting by default, as development is now encouraged via the SharePoint Framework (SPFx). Conversely, for classic sites, custom scripting is turned on by default.
​

When custom scripting is disabled, the following functionalities are blocked:

  • Many web parts, including the Content Editor and Script Editor, are disabled.

  • Uploading files that could potentially include scripts (e.g., .aspx, .html with embedded scripts) is blocked.

  • SharePoint Designer script changes are not allowed.

  • "Save Site as Template" and "Save document library as template" links are disabled.

  • The Solution Gallery, Sandbox Solutions, Theme Gallery, and HTML Field Settings are not available.

  • All other script-related features are turned off.

Note that using tenant-level settings, custom scripting for self-service sites can be enabled or disabled for all sites simultaneously. However, the PnP PowerShell module allows custom scripting to be enabled or disabled on a site-by-site basis, which is the focus of this guide.


PnP PowerShell to enable custom scripting

To enable custom scripting for a specific SharePoint Online site collection, use the following PnP PowerShell script.

# SCRIPT: Enable Custom Scripting for a SharePoint Online Site Collection

# --- 1. Import the SharePoint PnP PowerShell Online module ---
# This command ensures the necessary cmdlets are available.
# The '-Verbose' flag provides detailed information during module import.
Import-Module SharePointPnPPowerShellOnline -Verbose

# --- 2. Set the Site Collection URL ---
# IMPORTANT: Replace "https://contoso.sharepoint.com/sites/mysite" with your actual site collection URL.
$SiteURL="https://contoso.sharepoint.com/sites/mysite"

# --- 3. Connect to SharePoint Online ---
# This command will open a web browser prompt for you to log in to your Microsoft 365 account.
# Ensure you log in with a SharePoint Administrator account.
Connect-PnPOnline -Url $SiteURL -UseWebLogin

# --- 4. Enable Custom Scripting for the specified site ---
# The 'Set-PnPSite' cmdlet is used to modify site collection properties.
# Setting '-NoScriptSite $false' enables custom scripting on the site.
Set-PnPSite -Identity $SiteURL -NoScriptSite $false

Write-Host "Custom scripting has been enabled for site: $SiteURL"

Before running the script, ensure the following prerequisites are met:

  • Windows PowerShell v4.0 (or above)

  • SharePoint PnP PowerShell module

  • Windows Management Framework

Likewise, and considering the script accesses your SharePoint Administration Center, run it using a SharePoint Admin account.

After all dependencies have been sorted out, proceed to copy the PowerShell script above, pasting it into a newly created file with the .ps1 extension (e.g., EnableCustomScript.ps1).

For a comparison on how custom script is managed in SharePoint 2019 on-premises, please refer to our article: How to enable custom scripts in SharePoint 2019 (using PowerShell).

Did this answer your question?