Create a SharePoint Intranet

Subsites

A PowerShell with SharePoint tutorial

PowerShell icon
As a last part in our series PowerShell with SharePoint from Scratch, we will publish some articles and demos where Peter Kalmström explains how to create an intranet with three subsites for three departments in a small company.

The root site is a HQ site that we created in an earlier article. In this article, we will create subsites, one for each department. All sites are Communication sites, and they have a common navigation bar on top of each page.

Each department subsite will have its own apps, where team members are able to share documents, photos, appointments and tasks. The document library and the events list are created automatically when PowerShell creates a Communication subsite, but we will write code that creates the other two apps.

Each subsite will also have its own theme and three site pages. One of the pages will show a video.

In the demo below, Peter shows how to write PowerShell code that creates the three subsites - or webs, as they are called in the SharePoint API. He starts with creating an array for the three departments.

$Departments = "Sales","Production", "Support"

After that, Peter creates a forEach loop that runs through the array and creates subsites with the department names.

foreach($Dep in $Departments){
    #Create the subsite
    New-PnPWeb -Title $Dep -Url $Dep -InheritNavigation -Template SITEPAGEPUBLISHING#0
}

To find the correct Template ID, Peter uses the site https://www.technologytobusiness.com/microsoft-SharePoint/sharepoint-online-site-template-id

Peter also shows how to quickly remove all the subsites with another code sníppet. When you write and test code, it is important to have a smooth way to remove everything you have done from SharePoint to start over again. The removal code also uses a forEach loop and the same array as the creation code.
PowerShell with SharePoint from Scratch cover
foreach($Dep in $Departments){
    Remove-PnPWeb -Url $Dep -Force
}

You will find more detailed explanations in the book.




Download the script that Peter uses in the demo


In the next article, we will fix the common navigation.






Follow kalmstrom.com on LinkedIn Facebook or Twitter!

back icon next icon
Products Buy FAQ Services Tips Books Contact About Us Tools

Security and integrity

Copyright  Kalmstrom Enterprises AB  All rights reserved