Update Web Part Properties using PowerShell in SharePoint

Ever wanted to update SharePoint web part properties programmatically? After a company acquisition, We had to change the company name from a custom web part’s title, throughout the web application.

SharePoint update web part properties using PowerShell: 

$Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#configuration parameters
$WebURL="https://sharepoint.Crescent.com"

#Get objects
$web = Get-SPWeb $WebURL
$page = $web.GetFile("SitePages/Home.aspx")
$page.CheckOut()
$WebPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl,[System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

#$WebPartManager.WebParts
$WebPartManager.WebParts | % { write-host $_.title}
#Get the Web part
$Webpart = $WebPartManager.WebParts | Where { $_.Title -eq "Shared Documents"}

#Update Webpart Property
$Webpart.Title = "Team Documents"

#Save and Publish
$WebPartManager.SaveChanges($Webpart)
$page.CheckIn("updated Title Property") 

Update Web Part Properties in entire Web Application:
Say, we want to update all web part titles containing “Crescent Inc.” to “Lunar Inc.”

#Get All Webs (sites)
$webs = Get-SPWebApplication "https://sharepoint.Crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit All
 
 #Iterate through webs
 foreach ($web in $webs)
 {
  #Get All Pages from site's Root into $AllPages Array
  $AllPages = @($web.Files | Where-Object {$_.Name -match ".aspx"})
 
  #Search All Folders for Pages
  foreach ($folder in $web.Folders)
      {
          #Add the pages to $AllPages Array
          $AllPages += @($folder.Files | Where-Object {$_.Name -match ".aspx"})
      }

   #Iterate through all pages
   foreach($Page in $AllPages)
   {
        #Web Part Manager to get all web parts from the page
        $webPartManager = $web.GetLimitedWebPartManager($Page.ServerRelativeUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

       #Iterate through each web part
       foreach($webPart in $WebPartManager.WebParts)
       {
            $OldTitle=$webPart.title
            #Get the Content Editor web part with specific Title
            if($webPart.title -like "*Crescent Inc*")
             {
                  #Replace the Old Title
                  $webPart.title = $webPart.title.Replace("Crescent Inc.", "Lunar Inc.")
       
                  #Same method goes to update any other custom properties.
                  #E.g. To update Page viewer web part's link property:
                  #$webPart.ContentLink = "https://www.sharepointdiary.com" 
                  #To set built-it properties, E.g. To set Set the Chrome type programmatically use:
                  #$webPart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::TitleAndBorder
                  
                  #Save the changes
                  $webPartManager.SaveChanges($webPart)
                
                  write-host "Updated '$($OldTitle)' on $($web.URL)$($Page.ServerRelativeUrl)"
             }
         }
      }
  }

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “Update Web Part Properties using PowerShell in SharePoint

  • Hi,
    thanks for your great article. I have a question; is there any Solution for “Repleas an NewImage in SPO Website or SPO Template(.xml file)” ?
    Kind regards
    Farideh

    Reply
  • Hi, thanks for your great article.
    I have a question; is there any solution for “replace an image in SPO website or SPO Template .xml” ?
    Kind regards
    Farideh

    Reply
  • whats does the script looks like, when updating the contact webpart with contosousername to fabrikamusername . ?

    Reply

Leave a Reply

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