Written by

Ravishankar

Share

  

Category

see all categories
intranet as a service

Managing SharePoint Folders Using PnP PowerShell

In this article we are going to see how to perform operations like create, retrieve and delete folders on SharePoint libraries using PnP PowerShell scripts.

Creating folder in a SharePoint document library makes grouping and managing content easy to do. 

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 will help to connect SharePoint sites.  

$siteurl="https://<tenant-name>.sharepoint.com"   

Connect-PnPOnline -Url $siteurl  

PnPOnline-1

To Create A Folder on a library

Folders can be created on SharePoint library using Add-PnPFoldercmdlet on the SharePoint Site. The required parameters are,

Leaderboard_CDR

Folder - The parent folder in the site
Name -The folder name

The Optional parameters are,

Connection - Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.
Web - This parameter allows you to optionally apply the cmdlet action to a subweb within the current web. In most situations this parameter may not be required, and you can connect to the subweb using Connect-PnPOnline instead. Specify the GUID, server relative url (i.e. /sites/team1) or web instance of the web to apply the command to. Omit this parameter to use the current web.

The following code snippet helps to create folders on SharePoint Library

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

Documents-1

To Retrieve A Folder on a Library:

The Folders cane 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. In the case of a server relative url, make sure that the url starts with the managed path as the current web.

The Optional parameters are,

Connection - Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.
Web - This parameter allows you to optionally apply the cmdlet action to a subweb within the current web. In most situations this parameter is not needed, and you can connect to the subweb using Connect-PnPOnline instead. Specify the GUID, server relative url (i.e. /sites/team1) or web instance of the web to apply the command to. Omit this parameter to use the current web.
Includes - Specify properties to include when retrieving objects from the server.

The following code snippet helps to retrieve folder on SharePoint Library

Get-PnPFolder -RelativeUrl Shared%20Documents

pnp folder

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 optional parameters are,

Connection - Optional connection to be used by the cmdlet. Retrieve the value for this parameter by either specifying -ReturnConnection on Connect-PnPOnline or by executing Get-PnPConnection.
Web - This parameter allows you to optionally apply the cmdlet action to a subweb within the current web. In most situations this parameter is not required, and you can connect to the subweb using Connect-PnPOnline instead. Specify the GUID, server relative url (i.e. /sites/team1) or web instance of the web to apply the command to. Omit this parameter to use the current web.

The following code snippet helps to delete folder on SharePoint  Library

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

Remove-PnPfolderMy Documents

Hope you have learnt to create, retrieve, delete folders in the SharePoint library programmatically using PnP PowerShell scripting. The operations mentioned above were tested on SharePoint Online environment. Feel free to fill up the comment box below, if you need any assistance.