I want to get my Custom WebPart by name and add into a Page with PowerShell from (Web part gallery):

--> Edit Page --> Insert --> Web Part --> Custom

WebParts in image are added from Custom Feature(.wsp package) generated by Visual Studio

function AddWebPartsSolution($webAppURL){
    $userSolutionFileName = $Infrastructure.WebApplication.UserSolutionFile
    Add-SPSolution $scriptBase\Solution\$userSolutionFileName
    Install-SPSolution –Identity $userSolutionFileName –WebApplication $webAppURL –GACDeployment

function EnableWebPartsFeature($siteCollectionURL){
    #Get all Features -->  Get-SPFeature | Sort -Property DisplayName
    $featureName = $Infrastructure.WebApplication.UserSolutionName
    Enable-SPFeature -Identity featureName -Url $siteCollectionURL


enter image description here

I read this example, but PowerShell script get web part from local system. I need to get web part from image.

Where is the folder of Webparts from WebPart Gallery?

How to do this?

  • So for example, you want to add the calendar webpart to page ? Commented Jan 8, 2018 at 13:57
  • Custom web parts, seen my updated question. WebParts in image are from my Custom Feature generated by Visual Studio
    – Goshky
    Commented Jan 8, 2018 at 14:02

2 Answers 2


You can try and modify the below PS script:

#ensure that you use the site collection root url 
#if you use subsite url, will throw error since webpart gallery 
#exists only in the site collection root site

$web = Get-SPWeb "https://site-collection-url"

$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

$publishingPage = $pubWeb.GetPublishingPage("https://site-collection-url/Pages/Test.aspx");


$list = $web.Lists["Web Part Gallery"]   

#use the webpart title or the .webpart file name

$wpl = $list.Items | where {$_.Title -eq "Your Webpart Name"}   

$xmlReader = New-Object System.Xml.XmlTextReader($wpl.File.OpenBinaryStream());   

$errorMsg = ""   

$wpManager = $publishingPage.ListItem.File.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared);

$webPart = $wpManager.ImportWebPart($xmlReader, [ref]$errorMsg)   

#insert webpart into header zone at index 1


$publishingPage.CheckIn("Added webpart");




Modified from:

Web Parts Connections via powershell

Add webpart to publishing page using PS


Using the Microsoft PNP PowerShell Library, you can add WebParts to pages pretty easily.

For example to add from a webpart file:

Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -Path "c:\myfiles\listview.webpart" -ZoneId "Header" -ZoneIndex 1 

Or to add from XML where $webpart represents the XML:

Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -XML $webpart -ZoneId "Header" -ZoneIndex 1 

Adding to a wiki page has its own cmdlets but also can be added via path or XML:

Add-PnPWebPartToWikiPage -ServerRelativePageUrl "/sites/demo/sitepages/home.aspx" -XML $webpart -Row 1 -Column 1

The site in the link above has examples on how to connect using the PNP library and how to get the WebPartXML if you need it. Good luck!

  • I want to get webpart form Webpart gallery by name and add into a page.
    – Goshky
    Commented Jan 8, 2018 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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