SharePoint Online: How to Update Links in Word Documents using PowerShell?

Requirement: Update links in word documents in a SharePoint Online document Library

In a migration project, We had to replace all links embedded in Microsoft Word documents stored in SharePoint Online document libraries. Opening each document and updating the old links with a newer one would be cumbersome. Luckily, We can use PowerShell to bulk update all hyperlinks (and the link text) in all Microsoft Word documents stored in the SharePoint Online document library.

#Variables
$DomainURL = "https://crescent.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/Funds"
$LibraryName = "Documents"
$OldLink = "National"
$NewLink = "Crescent"

Try
{
    #Connect to SharePoint Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin
    $Web = Get-PnPWeb

    #Create a new COM object for word
    $Word = New-Object -ComObject Word.Application
    $Word.Visible = $False

    #Get all Word documents from the library
    $Documents = Get-PnPListItem -List $LibraryName -PageSize 500 | Where {$_.FieldValues.FileRef -like "*.doc*"}

    ForEach($Document in $Documents)
    {
        Try
        {
            $DocumentPath = $DomainURL + $Document.FieldValues.FileRef
            Write-host -f Yellow "Processing Document:"$DocumentPath
            #Open the Document
            $WordDocument = $Word.documents.Open($DocumentPath)

            #Get all links from the document
            $Hyperlinks = @($WordDocument.Hyperlinks)

            #Search and Replace Old Links and Text
             $Hyperlinks | ForEach-Object {
                If ($_.Address.ToLower().Contains($OldLink.ToLower()) -or $_.TextToDisplay.ToLower().Contains($OldLink.ToLower()))
                {
                    #Get the current Link and Link Text
                    $CurrentLink = $_.Address
                    $CurrentText = $_.TextToDisplay

                    #Update Link and Description
                    $_.Address =  $_.Address -Replace $OldLink, $NewLink
                    $_.TextToDisplay = $_.TextToDisplay -Replace $OldLink, $NewLink
                    $WordDocument.save()
                    $Document.Update()
                    Write-host -f Green "`tLink '$CurrentText' at '$CurrentLink' has been updated!"
                } 
            }
        }
        Catch {
            write-host -f Red "Document Update Error:" $_.Exception.Message
        }
        Finally {
            $WordDocument.Close()
        }
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}
Finally {
    $Word.quit()
}

This script will change the link text (description) as well as the link. You can wrap it inside a function to call for all document libraries in multiple sites.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “SharePoint Online: How to Update Links in Word Documents using PowerShell?

  • Can we do the same for xls* documents also?
    it will be helpful to know how we can update it for excel files also (including the formulas in all the sheets)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *