SharePoint Basic File Operations Using PnP PowerShell


Introduction

In this article, we will learn the basic file operations on SharePoint libraries, using PnP PowerShell. The operations like add, get, download, search, delete, check out and check in are explained here. 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  


Add New File

The files can be created and published, using Add-SPOFile command. The required parameters for the operation are the path and SharePoint folder URL. Other parameters like publish and publish comments can be used to publish the document with the comments.


The code snippet, given below, helps to add a file to SharePoint library.

  1. $newFile = Add-SPOFile -Path "D:\Nakkeeran\Files.docx" -Folder "/SPDocuments/Folder4" -Publish -PublishComment "Published1"

  2. Write-Host "Title : " $newFile. Name  

  3. Write-Host "URL   : " $newFile.ServerRelativeUrl  


The following snapshot shows the file added on to SharePoint folder, specified above.


Find or Retrieve File

The files can be searched and retrieved, using Find-SPOFile command. The required parameter for the operation matches. The matching text should be passed to retrieve the relevant files.

  1. $files = Find-SPOFile -Match *.docx  foreach($file in $files){      

  2. Write-Host "Title : " $file.Name     

  3.  Write-Host "URL   : " $file.ServerRelativeUrl      

  4. Write-Host "-----------------------------------"

  5. }  


Download File

Get-SPOFile command is used to download the file to the local machine. The required parameters are Server relative URL or the site relative URL of the file, local system path and local system file name. The code snippet, given below, helps in downloading SharePoint file to the specified local path.

  1. Get-SPOFile -ServerRelativeUrl /SPDocuments/Folder4/Files.docx -Path D:\Nakkeeran -FileName SharePointFile.docx  


Delete File

SharePoint files can be deleted from the site, using Remove-SPOFile command. The required parameter for the delete operation is the Server relative URL of the file. The code snippet, given below, deletes a file from the specified path without any confirmation prompts.

  1. Remove-SPOFile -ServerRelativeUrl "/SPDocuments/Folder4/Files.docx" -Force  


Check Out File

Set-SPOFileCheckedOut command is used to check out a file.


The operation, given below, helps to check out a file on SharePoint site.

  1. Set-SPOFileCheckedOut -Url "/SPDocuments/Folder4/Files.docx"


Check In File

Set-SPOFileCheckedIn command is used to check in the file with the user comments. The required parameter for the operation is the Server relative URL of the file, check in the type and check in the comments. The check in the type can be major (publish), minor or overwrite check in.


The operation, given below, helps to check in a file on SharePoint site.

  1. Set-SPOFileCheckedIn -Url "/SPDocuments/Folder4/Files.docx" -CheckinType MajorCheckIn -Comment "Check"

Summary

Thus, you have learned how to carry out the basic file operations on SharePoint folders programmatically, using PnP PowerShell commands. The operations like add, find, get, download, check in, check out, delete are explained above. 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.