Enable Custom Scripts in SharePoint with PowerShell

By default, script is allowed on most sites that admins create. It is not allowed on OneDrive, on sites users create themselves, on modern team and communication sites, and on the root site for your organization. You’ll probably want to limit the amount of script you allow for security reasons. For more info about the security implications of custom script, see Security considerations of allowing custom script.

There are two approaches to achieve this. In this post, we will cover the second approach using the PowerShell. In my last post I had used the first approach where we used SharePoint Admin Center to enable custom scripts. You can check out my previous post here.

Pre-requisites

  1. You must be a SharePoint admin or Microsoft 365 global admin to perform this action.
  2. SharePoint Online Management Shell

Video

Enable Custom Scripts using PowerShell

Once you have downloaded and installed the SharePoint Online Management Shell, open it from the Start button.

First you can check if you have already installed SharePoint Online Management Shell by running the following command in administrative mode in PowerShell:

Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select Name,Version

If the module is installed, then you will see the name of the module and the version.

Before enabling custom script, first we need to connect to SharePoint as global admin or SharePoint admin. To connect to SharePoint, we can either use username and password or if your organization has enabled MFA, then that also we can use.

To connect with a user name and password

Fill in the values for the $adminUPN and $orgName variables (replacing all the text between the quotes, including the < and > characters), and then run the following commands at the SharePoint Online Management Shell command prompt:

$adminUPN="<the full email address of a SharePoint administrator account, example: jdoe@contosotoycompany.onmicrosoft.com>"
$orgName="<name of your Office 365 organization, example: contosotoycompany>"
$userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential

When prompted with the Windows PowerShell credential request dialog box, type the password for the SharePoint admin account.

To connect with multifactor authentication (MFA)

Fill in the value for the $orgName variable (replacing all the text between the quotes, including the < and > characters), and then run the following commands at the SharePoint Online Management Shell command prompt:

$orgName="<name of your Office 365 organization, example: contosotoycompany>"
Connect-SPOService -Url https://$orgName-admin.sharepoint.com

When we are done with the connecting to SharePoint, run the following command to enable the Custom scripts.

Set-SPOsite <SiteURL> -DenyAddAndCustomizePages 0

To confirm if the Custom Scripts is enabled in the SharePoint, go to any of your SharePoint site -> Click on pages -> New -> Web Part Page.

Give any name and select any layout. Then click on Create.

Click on Add Web Part on any of the components. Click on Media and Content under Categories. You will see 3 new web parts added here.

Thank you All!!! Hope you find this useful.

Up ↑

%d bloggers like this: