Working With Folders On SharePoint Using PnP PowerShell

Introduction

In this article, we will learn how we can create, retrieve and delete the folders on SharePoint libraries, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite

You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation from the official site. The installers are available here. Online version installer is preferred for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).

The PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.

Connect To Site

Connect to the site, using the snippet, given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).

  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext   

Create SharePoint Folder

The folders can be created, using Add-SPOFolder command on SharePoint sites. The required parameters for creating a folder are: 

  • Name
  • Folder source URL

The code snippet, given below, helps in creating a folder on a SharePoint library, using PnP PowerShell. In this example, SPDocuments is the document library created on the site.

  1. Add-SPOFolder -Name "Folder1" -Folder "/SPDocuments"  

Ensure-SPOFolder is used to check if the folder exists. If it doesn’t exist, the folder will be created. This operation will return the object of the folder created/retrieved.

  1. Ensure-SPOFolder -SiteRelativePath "/SPDocuments/Folder3"   
Retrieve SharePoint Folders

The folders can be retrieved from SharePoint libraries, using PnP PowerShell. The filters like item type or item name can be used to trim the result set. Few properties like name, items count, folder URL can be retrieved from each of the items retrieved.

The code snippet, given below, retrieves all the root folders, available on the site.

  1. $folders = Get-SPOFolderItem -FolderSiteRelativeUrl "/"  

The snapshot. given below, shows the operation:

The code snippet, given below, helps in retrieving all the folders from SharePoint library.
  1. $folders = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments"  

The code snippet, given below, retrieves all the files under a specified folder.

  1. $files = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments/Folder1" -ItemType File  

The code snippet, given below, retrieves all the sub folders under a specified folder.

  1. $folders = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments" -ItemType Folder  

The code snippet, given below, retrieves item (folder) under the folder by item name.

  1. $folder = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments" -ItemName "Folder1"  

The code snippet, given below, shows accessing the properties of the folders.

  1. Write-Host "URL : " $folder.ServerRelativeUrl  
  2. Write-Host "Name: " $folder.Name  

The snapshot, given below, shows the folders created on the document library.

  
Delete SharePoint Folder

The folders can be deleted, using Remove-SPOFolder command, using PnP PowerShell. The required parameters for deleting the folder are folder URL and folder name. The code snippet, given below, helps in deleting a folder called "Folder3", which is present under "SPDocuments" library.

  1. Remove-SPOFolder -Folder "/SPDocuments" -Name "Folder3"  

Summary

Thus, you have learned, how to retrieve, create or delete SharePoint folders programmatically, using PnP PowerShell commands. PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The operations mentioned above, are tested on SharePoint 2013 and Office 365 environments.