Add And Manage SharePoint Files Using PnP PowerShell

In this post, I am going to show how to add and manage the document files in the SharePoint library using PnP PowerShell.

First, you will have to connect to your SharePoint Online Site using the following command.
 
Connect-PnPOnline -Url https://<Your-tenant-name>.sharepoint.com/sites/<your-site-name>
 

Add-PnPFile

 
The Add-PnPFile command is used to upload the document file to the SharePoint library.
 
Example 1
 
This example uploads the local file specified in the Local FilePath to the Shared Documents folder.
 
Add-PnPFile -Path "D:\NewFiles\PnPPowershell.docx" -Folder "Shared Documents"
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Example 2
 
This example uploads the local file specified in the Local FilePath to the Shared Documents folder with a different name.
 
Add-PnPFile -Path "D:\NewFiles\PnPPowershell.docx" -Folder "Shared Documents" -NewFileName "NewPnPPowershell.docx"
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Example 3
 
This example uploads the file PnPPowershell.docx to the Shared Documents folder. After uploading, it will set the modified date to 1/5/2019.
 
Add-PnPFile -Path "D:\NewFiles\PnPPowershell.docx" -Folder "Shared Documents" -Values @{Modified="1/5/2019"}
 
Add and Manage SharePoint Files Using PnP PowerShell
 

Copy-PnPFile

 
The Copy-PnPFile command is used to copy a file or folder to a different location.
 
Example 1
 
This example copies a file named PnPPowershell.docx located in a document library called NewDoc to a new document named NewPnPPowershell.docx in the same library.
 
Copy-PnPFile -SourceUrl NewDoc/PnPPowershell.docx -TargetUrl NewDoc/NewPnPPowershell.docx
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Example 2
 
This example copies a file named PnPPowershell.docx located in a document library called NewDoc in the current site to another site collection ModernSite.
 
Copy-PnPFile -SourceUrl NewDoc/PnPPowershell.docx -TargetUrl /sites/ModernSite/Doc/PnPPowershell.docx
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Note
If a file named PnPPowershell.docx already exists, it won't perform the copy operation.
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 

Find-PnPFile

 
The Find-PnPFile command is used to find a file in the file system of the web.
 
Example 1
 
Find-PnPFile -List "Documents" -Match *.docx
 
This example returns all the .docx files located in a given library.
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Example 2
 
Find-PnPFile -Folder "Shared Documents/SubFolder" -Match *.pdf
 
This example returns all the .docx files located in the given folder.
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Example 3
 
Find-PnPFile -Match *.docx
 
This example returns all the .docx files located in the current web.
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 

Get-PnPFile

 
The Get-PnPFile command is used to download a file.
 
Example 1 
 
Get-PnPFile -Url /sites/ModernSite/Doc/OnlineLicense.docx
 
This example retrieves the file and downloads it to the current folder.
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Example 2
 
Get-PnPFile -Url /sites/ModernSite/Doc/OnlineLicense.docx -Path D:\NewFiles -FileName OnlineLicense.docx -AsFile
 
This example retrieves the file and downloads it to D:\NewFiles.
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 

Remove-PnPFile

 
The Remove-PnPFile command is used to remove a file from the SharePoint library.
 
Example 1
 
Remove-PnPFile -SiteRelativeUrl Doc/PnPPowershell.docx
 
This example removes the file PnPPowershell.docx from the "Doc" document library.
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Example 2
 
Remove-PnPFile -SiteRelativeUrl Doc/OnlineLicense.docx -Recycle
 
This example removes the file PnPPowershell.docx from the "Doc" document library and saves it to the Recycle Bin.
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Output

 

Add and Manage SharePoint Files Using PnP PowerShell

 

Rename-PnPFile

 
The Rename-PnPFile command is used to rename a file in its current location.
 
Example 1
 
In this example, the command renames a file named PnPPowershell.docx located in the document library called Doc located in the site collection to NewPnPPowershell.docx.
 
Rename-PnPFile -ServerRelativeUrl /sites/ModernSite/Doc/PnPPowershell.docx -TargetFileName NewPnPPowershell.docx
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Add and Manage SharePoint Files Using PnP PowerShell
 
Note
If a file named NewPnPPowershell.aspx already exists, it won't perform the rename action.
 
Output
 
Add and Manage SharePoint Files Using PnP PowerShell