Automate site setup and library creation with PnP PowerShell
Setting up sites and libraries in SharePoint can be a tedious and repetitive process. Wouldn’t it be nice to be able to automate that process, so you don’t have to click the settings cog wheel or library settings over and over?
As you may know, PowerShell is a way to communicate and interact with SharePoint. PnP PowerShell, which stands for Patterns and Practices, was released a few years ago to make using PowerShell with SharePoint even easier.
PnP PowerShell is a library of pre-built commands that allows you to remotely interact with SharePoint to create, read, update, and delete data. Even better, it works for both SharePoint Online and on-premise.
SharePoint PnP PowerShell revolves around a few simple prefix commands of:
Add: To create something new, such a library, list, or view
Get: To retrieve a library, list, or view in order to do something to it, such as:
Remove: To delete what you retrieved
Set: To update the properties of what you retrieved
For example, to add a new library to a SharePoint site, the code looks like:
$LibraryTitle = “Contract Management"
$LibraryURL = “ContractManagement”
$newLibrary = New-PnPList -Title $LibraryTitle -Template DocumentLibrary -Url $LibraryURL -EnableContentTypes -OnQuickLaunch
During department site roll-outs for our clients, we build tens of sites with potentially hundreds of libraries within a short period of time.
Each library needs to have custom content types and views with associated custom columns in the correct sequence added. It also needs to have the default “Document” content type from the “+New” menu removed. This is a very manual and repetitive process, and since the process is the same for each library, we thought, why don’t we create a script to do that for us automatically?
Using SharePoint PnP PowerShell, our automation script allows you to:
Set the logo and theme of the site
Rename the generic Teams connected Documents library to “Teams Library”
Remove nodes from Quick Launch, such as “Site Contents” and “Pages”
Based on each line item in a user inputted CSV file:
Create a library and display it in the Quick Launch
Add specified content type(s) to the library and set the default content type
Create a new default view and add columns in the order specified
Remove the default “Document” content type from the Visible on New Button list
For each new department, the client’s IT administrator or the project team needs to:
Manually provision the desired site collection
Publish out required content types for the site (there’s a script for that too!)
Fill in the CSV files with the required library information
Have the logo file handy
Run the script
Sit back and relax!
With the help of this automation script, we were able to streamline our provisioning process and save hours of setup time per department. With the library settings coded in the CSV file, it also acts as documentation and gives the user a clear idea of which content type(s) are associated to which library. As a final benefit, there is less chance for error or missing something when sequencing the columns for the views.