Update Web Part on a Page Using Powershell in SharePoint 2010

I recently had to update a web part’s zone id that existed on most pages throughout the SharePoint 2010 site. I wrote up this powershell script to address this. DISCLAIMER: Use at your own risk!

  $site = new-object Microsoft.SharePoint.SPSite(“http://your_site”)
  foreach($web in $site.AllWebs)
{
    #Make sure you specify which page needs changes
    $page = $web.GetFile(“default.aspx”)
    $page.CheckOut()
    $wpm = $web.GetLimitedWebPartManager($page.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
  #$wpm.WebParts | ft
  $wp = $wpm.WebParts | Where-Object { $_.Title -eq “Page Tabs” }
  #$wp.ZoneID | ft
  $wp.ZoneID = “Header”
  $wpm.SaveChanges($wp)
  $page.CheckIn(“Update via PowerShell”,[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
  $wp.ZoneID | ft
}
$web.Close()
$site.Close()

I left the commented lines in since they can be helpful as well.