Official article: https://learn.microsoft.com/en-us/sharepoint/allow-or-prevent-custom-script#to-allow-custom-script-on-other-sharepoint-sites
So, here goes:
#gathering the tenant name
Write-Host “DenyAddAndCustomizePages checker and enabler/disabler”
write-host “=====================================================”
$tenantname = Read-Host “Enter your tenant’s name”
#connecting to the admin center
$tenantdadmin = “https://”+$tenantname+”-admin.sharepoint.com”
Write-Host “Connecting to your admin center at URL:” $tenandadmin -ForegroundColor Yellow
Connect-SPOService -Url $tenantdadmin
#Getting your site collection
$sitecol=Read-Host “Enter the URL of your site collection”
#checking the DenyAddAndCustomizePages setting
$verify1=(Get-SPOSite -Identity $sitecol).DenyAddAndCustomizePages
#doing verifications
Write-Host “The DenyAddAndCustomizePages setting for the site collection $sitecol is”: $verify1 -ForegroundColor Yellow
Write-Host “What actions to you want to take:”
$Choices = [System.Management.Automation.Host.ChoiceDescription[]] @(“&Enable”, “&Disable”, “&Cancel”)
$Default = 2
# Prompt for the choice
$Choice = $host.UI.PromptForChoice($Title, $Prompt, $Choices, $Default)
# Action based on the choice to Enable/Disable/Cancel
switch($Choice)
{
0 { Write-Host “Enable DenyAddAndCustomizePages”
Set-SPOSite -Identity $sitecol -DenyAddAndCustomizePages 1
Write-Host “Enabled” -ForegroundColor Green
}
1 { Write-Host “Disable DenyAddAndCustomizePages”
Set-SPOSite -Identity $sitecol -DenyAddAndCustomizePages 1
Write-Host “Disabled” -ForegroundColor Green}
2 { Write-Host “Cancel”
Write-Host “Canceled” -ForegroundColor Green}
}
Enjoy!
