aarebrot.net Frode's blog on Sharepoint and other stuff

Changing the properties of an existing Web Part using PowerShell

Posted on September 2, 2010
VN:F [1.9.22_1171]
Rating: 5.0/5 (3 votes cast)

Sometimes you need to change the properties of a Web Part without browsing to the page itself and set it. There could be numerous reasons as to why you would want or need to do this. A while back I created a really simple redirect web part for one of my clients. It had two custom properties, EnableRedirect and Url. If EnableRedirect was true, it would redirect the user to the specified Url. Obviously, if you want to change either of these properties while the EnableRedirect is true, that's going to be a problem.

Fast forward and the redirect web parts need to be changed and/or disabled. Obviously we can't navigate to the page and just change it, because the page will redirect you. But it can be done relatively easy using PowerShell.

Start out with opening the site and getting the file we want to work on. Also note that the site I'm working on is a publishing site, which is why we need to check out, check in and publish the page we want to change the web part on.

$site = new-object Microsoft.SharePoint.SPSite("http://sevm955603:15000")
$web = $site.OpenWeb("RandomSite/AnotherSite/")
$page = $web.GetFile("Pages/default.aspx")
$page.CheckOut()
$wpm = $web.GetLimitedWebPartManager("Pages/default.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

From here we can look up and see which web parts are currently active on the page.

$wpm.WebParts | ft

Which outputs the following:

Email       WebName     FeedbackLis Title         FrameType SuppressWeb Description  IsIncluded ZoneID        PartOrder
                        tName                                PartChrome
-----       -------     ----------- -----         --------- ----------- -----------  ---------- ------        ---------
Xxxxxxx@... /Xxxxxxx... Page Fee... Feedback        Default        True feedback           True Header                1
                                    Redirect        Default        True Redirect...        True RightColumn           0

There are two web parts on this page currently, and we want to change the second one. Let's finish of the script by doing this:

$wp = $wpm.WebParts[1]
$wp.EnableRedirect = $false
$wp.Url = "http://www.google.com"
$wpm.SaveChanges($wp)
$page.CheckIn("Test")
$page.Publish("Test")
$web.Close()
$site.Close()

And that's it. The redirect is turned off, and it's been changed to point to Google. It's not a work of art, and a little cruddier than what I usually like to make my scripts, but I needed something quick and dirty to do the job. And that's what this does.

Changing the properties of an existing Web Part using PowerShell, 5.0 out of 5 based on 3 ratings
Comments (3) Trackbacks (1)
  1. Thanks for your blog, it solved my burning problem :)

  2. Hi, I have a publishing site with quite a few web parts, I need to loop through the entire site and change the webpart description to the same as the title. This is due to the description showing non user friendly text for a website. How can I adapt your code to do this?

    • If you’re doing every single Web Part, it shouldn’t be too much of an issue. I haven’t tried it, but off the top of my head, instead of hard coding the value for $web, just grab the $site.AllWebs and iterate through that with a foreach loop. Then get the $page variable using GetFile like above and iterate through $wpm.WebParts with foreach to set the description to match the title for each WebPart.

      Let me know how that works out for you.


Leave a comment