Managing SharePoint Folders Using PnP PowerShell

In this article, we are going to see how to perform operations like create, retrieve, & delete folders on SharePoint libraries using PnP PowerShell scripts. Creating a folder in a SharePoint document library gives you an efficient way to group and manage your content.

Prerequisite

Before you begin utilizing PowerShell to oversee SharePoint Online, ensure that the SharePoint Online Management Shell is installed. You can install the SharePoint Online Management Shell by downloading and running the SharePoint Online Management Shell. You only need to do this once for each computer from which you are running SharePoint Online PowerShell commands.

Connect to Site

Connect to SharePoint site using "Connect-PnPOnline" cmdlet. The required parameters are,

  • Url -The SharePoint site URL (Eg: https://hubflysoft.sharepoint.com/sites/Hubfly)

The following code snippet helps to connect SharePoint sites.

  1. $siteurl="https://<tenant-name>.sharepoint.com"  
  2. Connect-PnPOnline -Url $siteurl  

SharePoint

To Create A Folder on a library

The folders can be created on SharePoint library using “Add-PnPFolder” cmdlet on the SharePoint Site. The required parameters are,

  • Folder - The parent folder in the site

  • Name - The folder name

The following code snippet helps to create the folder on SharePoint Library

  1. Add-PnPFolder -Name Folder1 -Folder Shared%20Documents  

 SharePoint

To Retrieve A Folder on a Library

The Folders can be retrieved on SharePoint library using “Get-PnPFolder” cmdlet on the SharePoint site. The required parameters are,

  • RelativeUrl - Site or server-relative URL of the folder to retrieve.

The Optional parameters are,

  • Includes - Specify properties to include when retrieving objects from the server.

The following code snippet helps to retrieve folder on SharePoint Library,

  1. Get-PnPFolder -RelativeUrl Shared%20Documents  

SharePoint

To Delete A Folder on a Library

The folders can be deleted on a SharePoint library by using “Remove-PnPFolder” cmdlet on the SharePoint site. The required parameters are,

  • Folder - The parent folder in the site

  • Name - The folder name

The following code snippet helps to delete a folder on SharePoint Library.

  1. Remove-PnPFolder -Name Folder1 -Folder Shared%20Documents -Force  

SharePoint

 

SharePoint

 

I hope you learned how to create, retrieve, delete folders in the SharePoint library programmatically using PnP PowerShell script. Feel free to fill up the comment box if you need any assistance.