How to remove domains within HTML Field Security settings via powershell

Spencer Cooper 11 Reputation points
2021-09-24T01:33:12.793+00:00

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 = "https://crescentintranet.sharepoint.com/sites/marketing"

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 = "products.crescent.com"
$Site.CustomScriptSafeDomains.Create($Domain)
Invoke-PnPQuery

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Spencer Cooper 11 Reputation points
    2021-09-26T07:35:01.86+00:00

    Solved this with the following code

    $removed= $Site.CustomScriptSafeDomains.GetByDomainName("test.com")
    $removed.deleteobject()

    1 person found this answer helpful.
    0 comments No comments

  2. Elsie Lu_MSFT 9,801 Reputation points
    2021-09-24T08:44:21.52+00:00

    Hi @Anonymous ,

    You could try to remove links manully:

    134994-72.png


    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.


  3. Limitless Technology 39,926 Reputation points
    2021-09-24T11:19:27.973+00:00

    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 : )


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.