I'm trying to get "webParts" from the client's site to see which ones are present and then delete the selected ones. The problem is that when the page is new the script does not find any elements. The strange thing is, that it finds elements only when someone makes some manual edit on Home page. I have all the permissions to the site.

I also tried the other way, i.e. adding a new web part to the page, but I got an error: Add-PnPClientSideWebPart : Index was out of range. Must be non-negative and less than the size of the collection. Here is the same situation: if someone manually edits the home page, I can already add new web parts

Connect-PnPOnline -Url $sourceUrl -Credentials $global:credentials
$page = Get-PnPClientSidePage -Identity "Home.aspx"
$webParts = $page.Controls

foreach ($webpart in $webparts) {
  • How is the new page added? Manually or via a script? Apr 15, 2021 at 16:20
  • Manually by another person
    – Jelon3k
    Apr 16, 2021 at 8:11
  • @DenisMolodtsov do you have idea why is happen ?
    – Jelon3k
    Apr 20, 2021 at 6:21

1 Answer 1



This command allows the retrieval of a modern sitepage along with its properties and contents on it. Note that for a newly created modern site, the Columns and Sections of the Home.aspx page will not be filled according to the actual site page contents. This is because the underlying CanvasContent1 will not be populated until the homepage has been edited and published. The reason for this behavior is to allow for the default homepage to be able to be updated by Microsoft as long as it hasn't been modified. For any other site page or after editing and publishing the homepage, this command will return the correct columns and sections as they are positioned on the site page.


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.