Update Hyperlink Field in SharePoint with PowerShell

Recently was asked to update the hyperlink field in a SharePoint list. The list had thousands of links pointing to a server we were moving, so the URL in the list needed to updated to reflect the new location. The only options I could think of, were to open SQL Server and do find/update against the content database(s). Well, Microsoft doesn’t like up playing with the SharePoint databases. So I cracked open PowerShell and went to town trying to figure this out.

#Add-PSSnapin Microsoft.SharePoint.PowerShell

$siteUrl = "http://sharepoint/SiteCollection/SiteName"
$webName = “SiteName”
$listName = "Name of your list"
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
$spWeb = $spSite.OpenWeb($webName)
$spList = $spWeb.Lists[$listName]

			foreach($Item in $spList.Items )
			{
				$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"])

				$ofldurl.URL = $ofldurl.URL.Replace("Nacho", "Taco")
				$ofldurl.Description = $ofldurl.Description.Replace("Nacho", "Taco")

				$item["URL"] = $ofldurl
				$item.update()
			}

$spWeb.Dispose()

What I’m doing here is replacing part of the URL string with a new word. The script is looking for the string Nacho and replacing it with Taco.

**** update ****
After migrating from SharePoint / WSS 2007 to SharePoint 2010, we started to notice some of our URLs were messed up. Web parts ( Page Viewer) and internal hyperlinks were pointing to an incorrect location.

example:
http://sharepoint/crmsp/customers/sitename/_layouts/1033/mngsubwebs.aspx?view=sites

Notice the 1033, no bueno!

Using the script above, i was able to update the links.

#Add-PSSnapin Microsoft.SharePoint.PowerShell

$siteUrl = "http://sharepoint/SiteCollection/SiteName"
$webName = “SiteName”
$listName = "Name of your list"
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
$spWeb = $spSite.OpenWeb($webName)
$spList = $spWeb.Lists[$listName]

			foreach($Item in $spList.Items )
			{
				$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"])

				$ofldurl.URL = $ofldurl.URL.Replace("1033/", "")
				$ofldurl.Description = $ofldurl.Description.Replace($ofldurl.Description, $ofldurl.Description)

				$item["URL"] = $ofldurl
				$item.update()
			}

$spWeb.Dispose()

In SharePoint 2016, the Description property appears broken?
This is the only way I could manage to set the URL and display text:

$newItem[“LinkField”] = "http://taco.com, taco2"

12 thoughts on “Update Hyperlink Field in SharePoint with PowerShell

  1. When I tried to run the script (having first modified it to point to things in my world), I received the following error:
    New-Object : Cannot find type [Microsoft.SharePoint.SPSite]: make sure the assembly containing this type is loaded.
    At Z:\extranets\fixurls.ps1:6 char:21
    + $spSite = new-object <<<< Microsoft.SharePoint.SPSite($siteurl)
    + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

    It seems I missing some software, any ideas on what? Thanks!

  2. I use the free PowerShell editor from Quest.
    http://www.quest.com/powershell/

    Make sure to add the add-pssnapin…
    Also try updating a sub-site.

    Your code should look something like this:

    Add-PSSnapin Microsoft.SharePoint.PowerShell
    
    $web = Get-SPWeb "sharepoint/sites/yourSubSite" 
    $spList = $web.Lists["Your List Name"]
    
    			foreach($Item in $spList.Items )
    			{
    				$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"])
    
    				$ofldurl.URL = $ofldurl.URL.Replace("1033/", "")
    				$ofldurl.Description = $ofldurl.Description.Replace($ofldurl.Description, $ofldurl.Description)
    
    				$item["URL"] = $ofldurl
    				$item.update()
    			}
    
    $Web.Dispose()
    
  3. Hi,

    We are getting the error “Value can not be null” at $item[“URL”] = $ofldurl.

    Any idea?

  4. What version of SP are you running?
    $ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item[“URL”])

    URL is the name of a field in the List.

  5. I get the same error as above… I can output the value of $ofldurl, and it is not NULL.

    Running PS 4.

    Was anyone able to get past this?

  6. @brian
    In SharePoint 2016, something must have changed. After messing with it for a bit, this is the only thing I could get to work:
    $newItem[“LinkField”] = “http://taco.com”
    $newItem.Update()

    Setting the description property would cause the update to fail.

  7. Hi,

    I am using SharePoint 2013 and the only way I could get it work is by converting the urlfield to string, please give a try this code:

    write-Host “deshabilitando eventos del workflow…”
    $prop.SetValue($null, $true, $null); #Disable event firing
    $tareaDerivada = $TareasDerivadasList.Items.GetItemById($itemAddedID);

    #Prepare the Hyperlink column for asignar tarea
    $linkAsignarTareaURL = $WebURL + “/Lists/derivacion/NewForm.aspx?IDInstructivoDerivacionPadre=” + $instructivoID + “|” + $itemAddedID
    $linkAsignarTarea = New-Object Microsoft.SharePoint.SPFieldURLValue
    $linkAsignarTarea.Description = “asignar tarea…”
    $linkAsignarTarea.URL = $linkAsignarTareaURL

    #Prepare the Hyperlink column for comentario
    $linkComentarioURL = $WebURL + “/SitePages/comentario.aspx?IDDerivacion=” + $itemAddedID
    $linkComentario = New-Object Microsoft.SharePoint.SPFieldURLValue
    $linkComentario.Description = “ver comentarios…”
    $linkComentario.URL = $linkComentarioURL

    $tareaDerivada[“linkComentario”] = $linkComentario.ToString()
    $tareaDerivada[“linkDerivar”] = $linkAsignarTarea.ToString()

    $tareaDerivada.Update();
    write-Host “habilitando eventos del workflow…”
    $prop.SetValue($null, $false, $null); #Disable event firing

  8. if you are encountering the null value issue “Value can not be null”
    based on the above example

    $ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item[“URL”])
    $ofldurl.URL = $ofldurl.URL.Replace(“1033/”, “”)
    $ofldurl.Description = $ofldurl.Description.Replace($ofldurl.Description, $ofldurl.Description)
    $item[“URL”] = $ofldurl
    $item[“URL”] = [Microsoft.SharePoint.SPFieldUrlValue]$ofldurl

  9. Thanks Jayaraja, I was struggling with Value cannot be null issue. Spent so many hours to resolve this issue. Your comment helped me resolved this issue.

    $link.URL = $link.Url.Replace($link.Url, $UpdatedStrUrl)
    if($link.Description -match $TextToFind)
    {
    $strDesc= $link.Description
    $UpdatedStrDesc= $strDesc.ToString().Replace($TextToFind,”VUE+InSphere+Model”)
    $link.Description = $link.Description.Replace($link.Description, $UpdatedStrDesc)
    Write-Host $UpdatedStrDesc
    }
    $item[$FieldName] = [Microsoft.SharePoint.SPFieldUrlValue]$link
    $item.Update()

Leave a Reply

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