SharePoint Online: Move a File between Document Libraries using PowerShell

Requirement: SharePoint Online PowerShell to Move files from one library to another.

How to move a File in SharePoint Online Document Library?

Are you looking for a way to move files between SharePoint Online sites? Maybe you need to move a file out of a document library and into your Files area? Or maybe you need to move a large number of files all at once?

In any case, Here is how to move files between document libraries in SharePoint Online:

  • Navigate to your SharePoint Online document library. Select the file to move >> Click on “Move To” in the toolbar. (Right-Click on a document and choose “Move To” does the same thing)
    sharepoint online powershell move documents
  • This opens the information panel on the right. Select the target library to which your file needs to be moved. You can select any folder in the current library, any other library on the current site, or even a library in any different site collections.
    powershell sharepoint online move files
  • Pick the target location to move the document and click on the “Move Here” button to start moving the file.
    move sharepoint online files with powershell

You’ll see the “Moving” message in the toolbar, and your file will be moved momentarily. You can use this method to transfer files between libraries or move files between site collections. Alright, Now let’s see how to use PowerShell to move files quickly!

Move SharePoint Online Files with PowerShell

Here is the SharePoint Online PowerShell to move documents between sites:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#powershell script to move files in sharepoint online - Function
Function Move-SPOFile([String]$SiteURL, [String]$SourceFileURL, [String]$TargetFileURL)
{
    Try{
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
        #sharepoint online powershell to move files
        $MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
        $Overwrite = $True
        [Microsoft.SharePoint.Client.MoveCopyUtil]::MoveFile($Ctx, $SourceFileURL, $TargetFileURL, $Overwrite, $MoveCopyOpt)
        $Ctx.ExecuteQuery()
 
        Write-host -f Green "File Moved Successfully!"
    }
    Catch {
    write-host -f Red "Error Moving the File!" $_.Exception.Message
    }
}
 
#Set Config Parameters
$SiteURL="https://Crescent.sharepoint.com/sites/Marketing"
$SourceFileURL="https://Crescent.sharepoint.com/sites/Marketing/Shared Documents/Discloser Asia.doc"
$TargetFileURL="https://Crescent.sharepoint.com/Shared Documents/Discloser Asia.doc"
 
#Get Credentials to connect
$Cred= Get-Credential
 
#Call the function to Move the File
Move-SPOFile $SiteURL $SourceFileURL $TargetFileURL

This PowerShell can be used to move files to another library or move files between site collections even.

PowerShell to Move Files Between Document Libraries of the Same Site:

You can use this PowerShell in SharePoint Online to move files to another library in a site.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL="https://crescent.sharepoint.com"
$FileURL="/Project Documents/Active Users.xlsx" #Server Relative Path to the source File
$DestFileURL="https://crescent.sharepoint.com/Project Docs/Active.xlsx"

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the File
$File = $Ctx.Web.GetFileByServerRelativeUrl($FileURL)

#sharepoint online powershell move documents
$File.MoveTo($DestFileURL, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite) 
$Ctx.ExecuteQuery()

PnP PowerShell to Move a File in SharePoint Online

Here is the PnP PowerShell to move files between folders in SharePoint Online: This PowerShell moves the file “Recipient KSA.pdf” from “Shared Documents” to the same library’s subfolder “Active”.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$SourceURL= "Shared Documents/Recipient KSA.pdf"
$TargetURL = "Shared Documents/Active/Recipient KSA.pdf"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#move files in sharepoint online using powershell
Move-PnPFile -SiteRelativeUrl $SourceURL -TargetUrl $TargetURL -Force

Similarly, you can move a file to another library or move a file between site collections as well. Please note, If the file name specified in Target URL already exists, it won’t perform the move. Use -OverwriteIfAlreadyExists to overwrite! The “AllowSchemaMismatch” switch ensures the target library can have a different set of metadata columns.

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$SourceFileURL= "Branding/Technical Design.docx" #Site Relative URL from the current site
$TargetFileURL = "/sites/Purchase/Shared Documents" #Server Relative URL of the Target Folder

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#move file between sites in sharepoint online using powershell
Move-PnPFile -SiteRelativeUrl $SourceFileURL -TargetUrl $TargetFileURL -Force -AllowSchemaMismatch

If you want to move all files between folders, use: SharePoint Online: Move All Files from One Folder to Another using PowerShell

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

6 thoughts on “SharePoint Online: Move a File between Document Libraries using PowerShell

  • Does this script move file with versions too?

    Reply
    • Yes! Move operation moves the file along with its versions. (But copy doesn’t!)

      Reply
  • Hi Saludeen!
    Thank you for so many useful tricks!! A couple of questions:
    1. The .MoveCopyUtil]::MoveFile creates the file in the destination as “new” (newly Created and LastModified fields) … is there a way to preserve this information?
    2. The Move-PnPFile cmdlet is not moving files across sites as per today (it errs out) do you know anything about it?

    Reply
    • Hi There,
      1. You can update the metadata after the move in CSOM, But confirmed the PnP PowerShell method preserves the metadata.
      2. Confirmed the PnP PowerShell to move a file between – just works fine as in the given script.

      Reply
  • What if you want to move more than one document? You have to explicitly call out each file?

    Reply
    • Call the “Move-SPOFile” function twice as in the First script with different parameters!

      Reply

Leave a Reply