SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to add a webpart to a great number of existing pages. Is there any PowerShell command to add a webpart to a webpart page?

share|improve this question
When you say "add a webpart to a SPWeb" do you mean you actually want to put the web part on a page within the SPWeb? Or you just want it to show up in the web part gallery for that SPWeb or ? – Sam Yates Jul 27 '11 at 22:29
Thanks for your commet! It mean that I want to put a existing WebPart from the WebPart gallery on a site – LaPhi Jul 28 '11 at 8:51
up vote 1 down vote accepted

There's no PowerShell Command, but you can use the AddWebPart method of the SPWebPartManager class to add the WebPart to a WebPartPage:

You can get a SPWebPartManager object by calling WebPartManager.GetCurrentWebPartManager and providing it your WebPartPage:

share|improve this answer

Theres nothing like this OOB, but you have pretty much the same options in PowerShell as in say a console application in C#.
So theres the option of coding your own cmdlet that does this for you.
On NBSP i found this script that should get you going.

share|improve this answer

Here are some sample powershell scripts for adding webparts to sites:

A nice, simple, clean code for adding a CEWP:

This one adds a customized web-part:

And this adds a page-viewer webpart - Mr. Hahner made this blogpost especially for me!

share|improve this answer
Whoops - I just realized that Anders Rask already mentioned one of my links... – bgmCoder May 6 '12 at 23:03

Here is a simple script to add a webpart to a page using PowerShell which is tested and working fine.

Please note that to add a webpart to a page export webpart to your local drive .webpart/.dwp file will be in xml format.


asnp "*sh*"

$web=Get-SPweb -Identity ""

[xml]$webpartxml= Get-Content -Path "C:\Manju\WPRequest.xml"

$SR = New-Object System.IO.StringReader($webpartxml.OuterXml)

$XTR = New-Object System.Xml.XmlTextReader($SR)


$WebPartZoneID = "Topzone"

$WebPartZoneIndex = 0




            Write-Host "Page is already Checkout to " $page.CheckedOutBy.UserLogin


            Write-Host "Page is Over ridded by " $web.CurrentUser.DisplayName + " to Add Webpart"


    $wmgr=$web.GetLimitedWebPartManager($page,    [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);




    $page.Publish('Adding request Site Webpart') 

    "Request Site WebPart SucessfullAdded" + (Get-Date -DisplayHint Date) | Out-File -Append "C:\OutPutLog.txt" 


        $ErrorMessage = $_.Exception.Message 

        "Request Site WebPart Failure" + $ErrorMessage  +  (Get-Date -DisplayHint Date) | Out-File -Append "C:\ErrorLog.txt"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.