Written by

Ravishankar

Share

  

Category

see all categories
intranet as a service

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 or 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 need to do this once only for each computer from which you will be 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

Add a File to SharePoint Library:

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

The required parameters are,

I. FileName- Name for file.
II. Folder- The destination folder in the site.
III. Path- The local file path.

The Optional parameters are,

Leaderboard_CDR

I. Approve - Will auto approve the uploaded file.
II. ApproveComment - The comment added to the approval.
III. CheckInComment - The comment added to the check in.
IV. Checkout - If versioning is enabled, this will check out the file first if it exists, upload the file and then check it in again.
V. 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.
VI. ContentType - Use to assign a Content Type to the file.
VII. Publish - Will auto publish the file.
VIII. PublishComment - The comment added to the public action.
IX. 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. Omit this parameter to use the current web.

The following code snippet will help  to add file on SharePoint library.

$libraryUrl = "Shared%20Documents"

Add-PnPFile -Path "F:\Ravishankar\Documents\IdeaTimeline.xlsx"  -Folder $libraryUrl

IdeaTimeline

The snapshots below show the file added to the SharePoint library

SharePoint library-1

Retrieve a File From SharePoint library:

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

I. FolderSiteRelativeUrlm - The site relative folder to retrieve.
II. ItemType - The type of contents to retrieve, either File, Folder or All (default).

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

Get-PnPFolderItem -FolderSiteRelativeUrl $libraryUrl -ItemType File

libraryUrl

Find a File From SharePoint library:

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

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

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

FolderSiteRelativeUrl

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,

I. AsFile - To download file as a file type.
II. Url - The URL (Server or Site relative) to the file.

The optional parameters are,

I. AsListItem - Returns the file as list Item showing all its properties.
II. AsString - Retrieve file content as string.
III. 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.
IV. FileName - Name for the local file.
V. Force - Overwrites the file if it already exists.
VI. Path - Local path where the file should be saved.
VVI. 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. Omit this parameter to use the current web. 

The following code snippet will help to download file from SharePoint library.

$filePath= $libraryUrl+ "/FileOne.docx"

$localPath= "F:\Ravishankar\Deployment\PSHELL\Output"

$localFileName = "NewFile.docx"

Get-PnPFile -Url $filePath -Path $localPath -Filename $localFileName -AsFile

Write-Host "File Downloaded Successfuly"

The snapshot below shows the file downloaded from SharePoint library.

Downloaded

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,

I. ServerRelativeUrl - Server relative URL for the file.
II. SiteRelativeUrl - Site relative URL.

The Optional parameters are,

I. 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.
II. Force - Overwrites the file if it already exist.
III. Recycle - Recycle the deleted
IV. 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. Omit this parameter to use the current web.

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

Remove-PnPFile -SiteRelativeUrl $filePath -Force

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

SiteRelativeUrl

Click here to download the full source code

Hope you have learnt how to add, retrieve, find and delete files 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.