Get Team sites & Create Document Library in SharePoint with PowerShell PnP

I needed to create a new document library to all our project related SharePoint Team sites. We use Office 365 Teams for both business units as for our projects, so first we need to get a list of all Team sites within the tenant, then check if the site is a project related Team site to finally add a new document library to it.

To get the job done we are going to use PowerShell PnP : more info about PnP

Getting all Team sites using PowerShell

To get all the team sites we need to connect to the SharePoint Admin Center to retrieve a list called : DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS

Connecting to the admin center

You can use one of mine connector scripts for it here at Technet. Or connect to SharePoint with:

Get the Team sites

The SharePoint Online site collection is stored in a hidden list, called DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS .  To retrieve the content of this list we first get all the available list items

get sharepoint teams lists

So in this case it’s the third item, so we use $web.lists[2] to get the teams sites

Check if it’s a project related Team sites

Now I wanted to now it the Team sites in the list is project related. All our project sites have the same document libraries, so the easiest way is to check if a particular document library exist:

We walk trough the list items, check if it as has SiteUrl, connect to the site with pnponline and stored credentials. Get the pnplist with the name ‘Document Library Name’, if it returns not null, then we can add the new document library.

Creating new Document Library

The last part is pretty easy, all we need to do is add a new Document Library to the Teams site. Just to be sure, we check first if the document library not already exists.

Thanks to Pieter Veenstra for retrieving the Team Sites 

And to Veronique Lengelle for the Create Document Library part