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.
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).
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$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:
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.
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.
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.
$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.
$folders = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments"
The code snippet, given below, retrieves all the files under a specified folder.
$files = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments/Folder1" -ItemType File
The code snippet, given below, retrieves all the sub folders under a specified folder.
$folders = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments" -ItemType Folder
The code snippet, given below, retrieves item (folder) under the folder by item name.
$folder = Get-SPOFolderItem -FolderSiteRelativeUrl "/SPDocuments" -ItemName "Folder1"
The code snippet, given below, shows accessing the properties of the folders.
Write-Host "URL : " $folder.ServerRelativeUrl
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.
Remove-SPOFolder -Folder "/SPDocuments" -Name "Folder3"
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.