Managing SharePoint Files Using PnP PowerShell

SharePoint library contains multiple file types like (.docx, .xls, .ppt) and folders. Here we are going to see how to add, find, retrieve, download & delete a file from SharePoint libraries using PnP PowerShell.

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

Add a File to SharePoint Library

The File can be added to SharePoint site by “Add-PnPFile” cmdlets on the SharePoint site. This cmdlet is applies to SharePoint Server 2013, SharePoint Server 2016, SharePoint Online.

The required parameters are,

  • FileName- Name for file.

  • Folder- The destination folder in the site.

  • Path- The local file path.

The Optional parameters are,

  • Approve - Will auto approve the uploaded file.

  • ApproveComment - The comment added to the approval.

  • CheckInComment - The comment added to the check in.

  • Checkout - If versioning is enabled, this will check out the file first if it exists, upload the file, then check it in again.

  • ContentType - Use to assign a Content Type to the file.

  • Publish - Will auto publish the file.

  • PublishComment - The comment added to the public action.

The following code snippet helps to add file on SharePoint library.

  1. $libraryUrl = "Shared%20Documents"
  2. Add-PnPFile -Path "F:\Ravishankar\Documents\IdeaTimeline.xlsx" -Folder $libraryUrl
SharePoint

The below snapshots shows the file added to the SharePoint library

SharePoint

Retrieve a File From SharePoint library

The file can be retrieved from the library using “Get-PnPFolderItem” cmdlet on the SharePoint site.This cmdlet applies to SharePoint Server 2013, SharePoint Server 2016, SharePoint Online.The required parameters are,

  • FolderSiteRelativeUrlm - The site relative folder to retrieve.

  • ItemType - The type of contents to retrieve, either File, Folder or All (default).

The following code snippets help to retrieve a file from SharePoint library

  1. Get-PnPFolderItem -FolderSiteRelativeUrl $libraryUrl -ItemType File
SharePoint

Find a File From SharePoint library

The file can be found in the SharePoint library by passing “-ItemName” parameter in “Get-PnPFolderItem” cmdlet on the SharePoint site.

The following code snippets help to find a file from a SharePoint library

  1. $file=Get-PnPFolderItem -FolderSiteRelativeUrl $libraryUrl -ItemType File -ItemName "FileOne.docx"
SharePoint

Download a File From SharePoint Library

The file can be downloaded from SharePoint library using “Get-PnPFile ” cmdlets on SharePoint site. The required parameters are,

  • AsFile - To download file as a file type.

  • Url - The URL (Server or Site relative) to the file.

The optional parameters are,

  • AsListItem - Returns the file as list Item showing all its properties.

  • AsString - Retrieve file contents as string.

  • FileName - Name for the local file.

  • Force - Overwrites the file if it already exist.

  • Path - Local path where the file should be saved.

The following code snippets helps to download file from SharePoint library

  1. $filePath= $libraryUrl+ "/FileOne.docx"
  2. $localPath= "F:\Ravishankar\Deployment\PSHELL\Output"
  3. $localFileName = "NewFile.docx"
  4. Get-PnPFile -Url $filePath -Path $localPath -Filename $localFileName -AsFile
  5. Write-Host "File Downloaded Successfuly"

The below snapshot shows the file downloaded from SharePoint library.

SharePoint

Delete a File From SharePoint library

The file can be deleted from SharePoint library using “Remove-PnPFile” on the SharePoint site. The required parameters are,

  • ServerRelativeUrl - Server relative URL for the file.

  • SiteRelativeUrl - Site relative URL.

The Optional parameters are,

  • Force - Overwrites the file if it already exists.

  • Recycle - Recycle the deleted file.

The following code snippets help to delete a file from SharePoint library.

  • Remove-PnPFile -SiteRelativeUrl $filePath -Force 

The below snapshot shows the file is deleted from the SharePoint library

SharePoint

Hope you learned how to add, retrieve, find & delete files in the SharePoint library programmatically using PnP PowerShell scripting. The operations mentioned above are tested on SharePoint Online environment. Feel free to fill up the comment box below, if you need any assistance.