Manipulating webparts using PowerShell

Hello All,

One of my customers is making some changes to there Sites and part of that is to set the owner in a Contact Details webpart, since it is for several site we went and figured out the PowerShell.  The customer changes the title from 'Contact Details' to 'Site Owner'.

I wrote this without Error Checking as this was provided simply as an example, so I would suggest you add that yourself.

Add-PSSnapin Microsoft.SharePoint.PowerShell

$WebAppURL = "https://sp2013app01"
$SiteUrl = "https://sp2013app01/sites/gremlins"
$PageName = "default.aspx"
$WebPartTitle = "Site Owner"
$SiteOwnerLogin = "weaver\administrator"

$Page = (Get-SPSite $SiteUrl).RootWeb.GetFile($PageName)
$WebPartMgr = $Page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$WebPart = $WebPartMgr.WebParts | where{$_.Title -eq $WebPartTitle}
$webPart.ContactLoginName = $SiteOwnerLogin
$WebPartMgr.SaveChanges($webPart)

The key to this is the line.

$WebPartMgr = $Page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

Afterwards we are able to retrieve all the webparts and manipulate the property 'ContactLoginName' which is not obvious as the GUI shows Contact and there is a property named Contact as well.

Finally we run the method SaveChanges to complete everything.

Pax