I would like to get all the links details inside teaser ,richtext editor components etc through Powershell script.

Is it possible through Powershell script ?


4 Answers 4


You can use HtmlAgilityPack for parsing Html like below. I didn't test this, you can give it a try.

$html = @"
<div class="kitchen">
   <div class="kitchen">
        <blockquote>kitchen<br />
            <span class="kitchen">kitchen</span>
        <a href='test'><img title="kitchen" src="https://kitchen-sink.local" /></a>

$htmlDocument = New-Object -TypeName HtmlAgilityPack.HtmlDocument
foreach($x in $htmlDocument.DocumentNode.Descendants()) {
    if($x.Name -eq "//a[@href]") {
        //your logic here

You can achieve it using RegEx as well- First, you need to get the item, and then from the item, you need to get the field value. here below example will work fine for a single item but if you need it for multiple items you need to perform a loop, for example, getting children of a root item.

$anchorTagPattern = '(?is)<a[^>]*>(.*?)<\/a>';

$hrefPattern = '(?is)href=\"[^>]*(.*?)\"';

#get item by item id i.e {BB87FEE6-E4CF-4D86-986B-2D1B297A081F}
$item = Get-Item -Path "master:" -ID "{BB87FEE6-E4CF-4D86-986B-2D1B297A081F}"

#get field value by field name i.e Content
$htmlContent = $item.Fields["Content"].Value;

$anchorTagPatternMatches = [regex]::Matches($htmlContent, 

if(![string]::IsNullOrEmpty($anchorTagPatternMatches)) {
    $anchorTagPatternMatches | ForEach-Object {
        $anchorText = $_.Groups[1].Value;
        $anchorTag = $_[0].Value;
        write-host $anchorTag
        write-host $anchorText
        $hrefPatternMatches = [regex]::Matches($anchorTag, $hrefPattern);
        $href = $hrefPatternMatches[0].Value;
        $href = $href.Replace('href=','');
        write-host $href
  • How can I get html of the item in PowerShell Script?
    – Nisha
    Sep 5, 2022 at 17:49
  • I have updated my answer here Sep 6, 2022 at 8:16

For the Rich Text Editor field, you can use this PowerShell script and get all the links.

# Get your item here by providing ID
$rootItem = Get-Item -Path "master:" -ID "{56305117-8C9C-4C08-96D5-BB97A8E27419}"

# Regex pattern to check if the value contains any anchor
$anchorTagPattern = '(?is)<a[^>]*>(.*?)<\/a>';

# Get your RTE Field 
$richTextContent = $rootItem.Fields["DB"].Value;

# Match parttern with your RTE field value
$anchorTagPatternMatches = [regex]::Matches($richTextContent, $anchorTagPattern);

# If Pattern matches 
if(![string]::IsNullOrEmpty($anchorTagPatternMatches)) {

# Create a loop and it will give you all the anchors in your RTE
ForEach ($patterns in $anchorTagPatternMatches) {
       # You will get the values of anchors using this. 
       Write-Host $patterns

The output you will get is this.

<a href="http://test.com">newLink</a>
<a href="http://google.com">newLink</a>
<a href="http://google.com">Google</a>

And you can get other values as well.

enter image description here

For any other field, you can pass your field name like below and get the details of the links.

$richTextContent = $rootItem.Fields["your field"].Value;

For more details refer to this link.

How to update the anchor link text in rich text field using Sitecore Powershell?


You can get the link detail on the page by using the code below :-

$pages = get-item -Path "<item path>"
$getPageLinks = $page.Fields | where {$_.Type -eq 'General Link'}
            ForEach ($link in $getPageLinks) 
              [Sitecore.Data.Fields.LinkField] $field = $link
              if(-not [string]::IsNullOrEmpty($field))
                        <your code>

Related to get the link details of the components on the page, you need to get the rendering of the page first and based on it you can get the links. I am sharing the example of richtext editor here.

$renderings = Get-Rendering -Item $page -Device $device -FinalLayout 
    foreach($rendering in $renderings){
        if($rendering.ItemID -ne $null -and $rendering.Datasource -ne "")
            $renderingItem = Get-Item master: -ID $rendering.ItemID  -ErrorAction Ignore
            if($renderingItem -ne $null) {
                $renderingDataItemPath = "master:" + $rendering.Datasource
                $renderingDataItem= Get-Item -Path $renderingDataItemPath -ErrorAction Ignore
                $getRichTextDetail=$renderingDataItem.Fields | where {$_.Type -eq 'Rich Text'}
                                if(-not [string]::IsNullOrEmpty($getRichTextDetail.Value))
                                    $fieldValueWithHtml = $getRichTextDetail.Value
                                    if($fieldValueWithHtml -ne $null){
                                        $source = [System.Web.HttpUtility]::HtmlDecode($fieldValueWithHtml)
                                        $html = New-Object -ComObject "HTMLFile";
                                        $html.IHTMLDocument2_write($source); # Reading in to a proper HTML document
                                        $html.Links | % {
                                         if(-not [string]::IsNullOrEmpty($_.href))
                                                <your logic here>

Hope this helps!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.