How to remove domains within HTML Field Security settings via powershell

Rowdey Cooper 201 Reputation points

As the title states, I am working on a script and need to have the ability to remove links from the allowed domains section of HTML Field Security of Sharepoint Online.....

as an example the below code successfully adds links.

Set Variables

$SiteURL = ""

Connect to PnP Online

Connect-PnPOnline -Url $SiteURL -UseWebLogin

Get the Site

$Site = Get-PnPSite -Includes CustomScriptSafeDomains

Add domain

$Domain = [Microsoft.SharePoint.Client.ScriptSafeDomainEntityData]::new()
$Domain.DomainName = ""

A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
7,073 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
4,891 questions
0 comments No comments
{count} votes

2 additional answers

Sort by: Most helpful
  1. Elsie Lu_MSFT 9,726 Reputation points

    Hi @Rowdey Cooper ,

    You could try to remove links manully:


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

  2. Limitless Technology 37,771 Reputation points

    Hello RowdeyCooper,

    Your code seems completely fine to me.

    But before code execution Add Trusted Domain To HTML Field security List using the below step.

    Online Site Collection >> Site Settings >> Select "HTML Field Security" from Site Collection Administration.

    Choose Allow Contributors to insert iframe" Option

    Add your preference and Click Ok.

    and consider Switching On the Custom Script before the process.

    Hope this answers all your queries, if not please do repost back.
    If an Answer is helpful, please click "Accept Answer" and upvote it : )