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.

We have a skilled SharePoint team and if you need some help implementing your intranet, reach out to us.