kwizcom banner advertisement

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?
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 information panel in the right. Select the target library to which your file needs to be moved. You can select any folder in current library, any other library in 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 "Move Here" button to start moving the file.
    move sharepoint online files with powershell
You'll see the "Moving" message in tool bar and your file will be moved momentarily. You can use this method to move files between libraries or move files between site collections.

Move SharePoint Online Files with PowerShell
Here is the SharePoint Online PowerShell to move documents
#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"
 
#Function to Move a File
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)
     
        #Move the File
        $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://crescenttech.sharepoint.com/sites/Marketing"
$SourceFileURL="https://crescenttech.sharepoint.com/sites/Marketing/Shared Documents/Discloser Asia.doc"
$TargetFileURL="https://crescenttech.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 file 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" #Relative Path to the source
$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)      
$File.MoveTo($DestFileURL, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite) 
$Ctx.ExecuteQuery()

PnP PowerShell to Move a File in SharePoint Online
#Config Variables
$SiteURL = "https://crescenttech.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 a File
Move-PnPFile -SiteRelativeUrl $SourceURL -TargetUrl $TargetURL -Force
Similarly, you can move 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!
SharePoint Online: Move a File between Document Libraries using PowerShell SharePoint Online: Move a File between Document Libraries using PowerShell Reviewed by Salaudeen Rajack on March 18, 2018 Rating: 5

1 comment:

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

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.