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"