SharePoint Pages and Web Parts with PowerShell

To create a SharePoint page using PowerShell, you can use the New-SPWebPartPage cmdlet. This cmdlet creates a new Web Part page in the specified document library, with the specified name and layout.

SharePoint PowerShell

Here is an example of how to use this cmdlet to create a new Web Part page in a document library called “Pages” in the root site of a SharePoint site:

$web = Get-SPWeb http://yoursite $library = $web.Lists["Pages"] $page = $library.RootFolder.Files.Add("page.aspx", [Microsoft.SharePoint.SPTemplateFileType]::WebPartPage) $web.Dispose()

To add a Web Part to the page, you can use the AddWebPart method of the SPLimitedWebPartManager class. This method takes two arguments: the Web Part to add, and a zone to add it to.

Here is an example of how to add a Web Part to the page that was created in the previous example:

$web = Get-SPWeb http://yoursite $page = $web.GetFile("/Pages/page.aspx") $manager = $page.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) $webPart = New-Object Microsoft.SharePoint.WebPartPages.ContentEditorWebPart $webPart.Content = "Hello, world!" $manager.AddWebPart($webPart, "Main", 1) $web.Dispose()

This will add a Content Editor Web Part to the page, with the content “Hello, world!”.

We hope this helps!

Are you starting with SharePoint Online PowerShell

To use SharePoint Online PowerShell commands, you must have the SharePoint Admin role or Global Administrator role in Microsoft 365.

You must install the SharePoint Online Management Shell module and connect to the SharePoint Online service. For more information, see Connect SharePoint Online.

Ready to enhance your SharePoint experience?

Contact us for:

  • Intranet and Portals: Personalized solutions just for you.
  • Application Development: Intuitive, scalable, and secure solutions.
  • Consultancy Services: Expert advisory solutions.
  • Outsourcing: Maximize productivity.
  • Support and Maintenance: Keep your applications running seamlessly.
 

If you are interested in learning more about us and how we can help you, contact us. 

You can also check out our blog for more articles and insights on  Microsoft 365 technologies.