SharePoint Online: Move All Files from One Folder to Another using PowerShell
Requirement: Move All Files Between Folders in SharePoint Online using PowerShell.
How to move files between folders in SharePoint Online?
In SharePoint Online, folders play a crucial role in organizing and categorizing content, making it easier to find what you need. However, as your organization grows and evolves, the need to move files between folders may arise. This can be done manually by selecting the files and choosing “Move to” through the SharePoint user interface. However, if you have multiple files to move, r repeatedly move files, this process can become time-consuming. Fortunately, there is a method to automate the task of moving files between folders in SharePoint Online using PowerShell. In this guide, we will explore both methods and show you how to move files between folders in SharePoint Online.
To move files between folders in SharePoint Online with SharePoint UI, you can perform the following steps:
- Navigate to the source folder in SharePoint Online and select the file you want to move.
- Select the files to move and click on the “Move to” button from the command bar.
- Select the destination folder, where you want to move the file to.
- Click on the “Move here” button.
This is the easiest and most straightforward method to move files between folders in SharePoint Online.
PowerShell to Move All Files from One Folder to Another in SharePoint Online:
If you’ve been using SharePoint Online for any length of time, you’re probably familiar with the basic file upload and download process. However, there may be times when you need to move files between folders on your site. When you are frequently moving files and folders around in SharePoint Online, It can be a little cumbersome to open the SharePoint Online site, navigate to the folder where you want to move the file or folder, and then move it into place.
Wouldn’t it be nice if there was an easy way to do this from PowerShell? This article will show you how to use PowerShell to quickly and easily move files between folders in SharePoint Online. Let’s get started!
How to move files between folders in SharePoint Online? Well, here is the PowerShell to move all files from one folder to another folder:
#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"
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"
#Relative URLs of Source and Destination Folders
$SourceFolderURL = "/sites/marketing/Shared Documents"
$TargetFolderURL = "/sites/marketing/Shared Documents/Archive"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get all items from source folder
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$Query.FolderServerRelativeUrl = $SourceFolderURL
$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
#Move All Files from Source to Destination
ForEach ($Item in $ListItems)
{
#Filter Files
If($Item.FileSystemObjectType -eq [Microsoft.SharePoint.Client.FileSystemObjectType]::File)
{
$TargetFileUrl = $Item["FileRef"].ToString().Replace($SourceFolderURL,$TargetFolderURL)
$Item.File.MoveTo($TargetFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
$Ctx.ExecuteQuery()
Write-host -f Green "File Moved to:"$TargetFileUrl
}
}
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
This script moves all files from the source folder to the target folder. What if the source folder has sub-folders?
SharePoint Online: Move Files and Folders using PowerShell
This PowerShell script moves all files and sub-folders between given folders in a SharePoint Online document library. We can also move a folder between document libraries or sites with PnP PowerShell, as in How to move a Folder in SharePoint Online?
#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 Move-SPOFilesBetweenFolders
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
[Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
)
Try {
#Write-host "Copying Files from '$($SourceFolder.ServerRelativeUrl)' to '$($TargetFolder.ServerRelativeUrl)'"
#Get all Files from the source folder
$SourceFilesColl = $SourceFolder.Files
$Ctx.Load($SourceFilesColl)
$Ctx.ExecuteQuery()
#Iterate through each file and move
Foreach($SourceFile in $SourceFilesColl)
{
#Get all files from source Folder
$SourceFile =$Ctx.Web.GetFileByServerRelativeUrl($SourceFile.ServerRelativeUrl)
$Ctx.Load($SourceFile)
$Ctx.ExecuteQuery()
#Move File to destination
$TargetFileUrl = $SourceFile.ServerRelativeUrl -Replace $SourceFolderURL,$TargetFolderURL
$SourceFile.MoveTo($TargetFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)
$Ctx.ExecuteQuery()
Write-host -f Green "File Moved to: "$TargetFileURL
}
#Process Sub Folders
$SubFolders = $SourceFolder.Folders
$Ctx.Load($SubFolders)
$Ctx.ExecuteQuery()
Foreach($SubFolder in $SubFolders)
{
If($SubFolder.Name -ne "Forms")
{
#Prepare Target Folder
$EnsureFolderURL = $SubFolder.ServerRelativeUrl -Replace $SourceFolderUrl, $TargetFolderUrl
Try {
$Folder=$Ctx.web.GetFolderByServerRelativeUrl($EnsureFolderURL)
$Ctx.load($Folder)
$Ctx.ExecuteQuery()
}
catch {
#Create Folder
if(!$Folder.Exists)
{
$Folder=$Ctx.Web.Folders.Add($EnsureFolderURL)
$Ctx.Load($Folder)
$Ctx.ExecuteQuery()
Write-host "New Folder Created:"$SubFolder.Name -f Yellow
}
}
#Call the function recursively to move all files from source folder to target
Move-SPOFilesBetweenFolders -SiteURL $SiteURL -SourceFolder $SubFolder -TargetFolder $Folder
#Remove the Source Folder
$SubFolder.Recycle() | Out-Null
$Ctx.ExecuteQuery()
}
}
}
Catch {
write-host -f Red "Error Moving File:" $_.Exception.Message
}
}
#Set Parameter values
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$SourceFolderURL ="/sites/marketing/shared Documents/2018"
$TargetFolderURL ="/sites/marketing/Shared Documents/2019"
#Setup Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the source and Target Folders
$SourceFolder=$Ctx.Web.GetFolderByServerRelativeUrl($SourceFolderURL)
$Ctx.Load($SourceFolder)
$TargetFolder=$Ctx.Web.GetFolderByServerRelativeUrl($TargetFolderURL)
$Ctx.Load($TargetFolder)
$Ctx.ExecuteQuery()
#Call the function
Move-SPOFilesBetweenFolders -SiteURL $SiteURL -SourceFolder $SourceFolder -TargetFolder $TargetFolder
If you want to move a single file, use: SharePoint Online: Move a File between Document Libraries using PowerShell
PnP PowerShell to Move All Files and Folders Between Document Libraries
PnP PowerShell makes it much simpler! You can move document library contents to another SharePoint Online site with this PowerShell script:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Purchase"
$SourceLibraryURL = "Migration" #Site Relative URL from the current site
$TargetLibraryURL = "/sites/Marketing/Migration" #Server Relative URL of the Target Folder
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get all Items from the Document Library
$Items = Get-PnPFolderItem -FolderSiteRelativeUrl $SourceLibraryURL | Where {$_.Name -ne "Forms"}
#Move All Files and Folders Between Document Libraries
Foreach($Item in $Items)
{
Move-PnPFile -SourceUrl $Item.ServerRelativeUrl -TargetUrl $TargetLibraryURL -AllowSchemaMismatch -Force -AllowSmallerVersionLimitOnDestination
Write-host "Moved Item:"$Item.ServerRelativeUrl
}
Make sure you have the Target library created before running this script!
Conclusion:
In summary, SharePoint has the ability to organize content into folders, making it easy to find what you need. When it is necessary to move files between folders, it can be done in a few clicks, as moving files between folders in SharePoint Online has been made much easier now! It is also possible to automate this task using PowerShell, as it is more efficient. With just a few lines of code, you can automate the process of organizing your content.
Do you have a way to do the PnP PowerShell to Move All Files and Folders Between Document Libraries from a csv?
Hello!
I’d like to make some changes to the script “SharePoint Online: Move Files and Folders using PowerShell”, but I don’t know how exactly I can do that and would be very grateful for help.
– The script shouldn’t overwrite files (I managed to do that actually).
– The script shouldn’t stop if the file already exists on the destination and continue with the next file.
– Somehow the script doesn’t work for subfolders for me, but I’ve made changes to the script, so that may be the reason.
Thanks in advance!
I’ve added -Recursive to line 10 and its doing subfolders, however, I get an error for every subdirectory, and file within subdirectories despite it actually moving the folder/file.
$Items = Get-PnPFolderItem -FolderSiteRelativeUrl $SourceLibraryURL -Recursive | Where {$_.Name -ne “Forms”}
Error is:
‘The system cannot find the file specified. (Exception from HRESULT: 0x80070002)’
Thanks for providing this script it looks great.
Can the same script be used for moving files to a different site in the same tenant?
Also is there a way to validate the number of files in the source (including versions) and the number moved to the destination? I guess I’m looking for logging that can display any errors encountered in the move?
Thank you for this, do you have a way to move files to a folder with existing files on it?
Do you mean, You want to avoid overwriting the file if that exists already? Use the flag [Microsoft.SharePoint.Client.MoveOperations]::none