SharePoint Online: How to Move a Folder using PowerShell?
Requirement: Move a Folder to Another Library or Another Site in SharePoint Online.
How to Move a Folder in SharePoint Online?
Moving folders is one of the most common SharePoint Online tasks. This article will show you how to move a folder using PowerShell and the web browser interface. This can be handy if you need to reorganize your content or if the folder has become too large and is causing performance issues. Let’s get started!
Moving a folder in SharePoint Online’s modern experience is quite simple! Follow these step-by-step instructions for moving folders in SharePoint Online:
- Navigate to your SharePoint Online library containing the folder you want to move >> Select the Folder.
- From the Toolbar, click on the “Move to” button.
- Select the destination location you want a move the folder. You can even choose a different site collection or subsite. Use “Browse Sites” to see a complete list of sites you can move to.
- You can also create a new folder in the destination. Click on the “Move here” button to start moving the folder.
This moves the folder with all its files to the selected destination. Similarly, You can move a folder to another folder, another library, or even another site or site collection. The same methods described above are applicable when you want to copy a folder in SharePoint Online (use “Copy to” instead of “Move to”!). Please note, the “Move To” is not available in the SharePoint Online classic experience.
Just select the folder by clicking on its name >> Click and hold the folder, then drag it to the destination library or folder where you want to move it >> Release the mouse button to drop the folder into the new location.
SharePoint Online: Move a folder using PowerShell
Moving folders around in SharePoint is a pretty simple process through the web browser interface, isn’t it? We can also use PowerShell to move a folder in SharePoint Online. Let’s move the folder up a level in a SharePoint Online document library:
#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 Folder
Function Move-SPOFolder([String]$SiteURL, [String]$FolderSourceURL, [String]$FolderTargetURL )
{
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 Folder
$MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions
#$MoveCopyOpt.KeepBoth = $True
[Microsoft.SharePoint.Client.MoveCopyUtil]::MoveFolder($Ctx, $FolderSourceURL, $FolderTargetURL, $MoveCopyOpt)
$Ctx.ExecuteQuery()
Write-host -f Green "Folder Moved Successfully!"
}
Catch {
write-host -f Red "Error Moving the Folder!" $_.Exception.Message
}
}
#Set Config Parameters
$SiteURL="https://crescent.sharepoint.com/"
$FolderSourceURL="https://crescent.sharepoint.com/ProjectDocs/Active/2018"
$FolderTargetURL="https://crescent.sharepoint.com/ProjectDocs/2018"
#Get Credentials to connect
$Cred= Get-Credential
#Call the function to Move the Folder
Move-SPOFolder $SiteURL $FolderSourceURL $FolderTargetURL
The folder has now been moved to its new location within SharePoint.
PnP PowerShell to Move a Folder in SharePoint Online:
You can use the Move-PnPFolder cmdlet to move a folder to a different library or the same library in SharePoint Online.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$SourceFolderURL= "Shared Documents/2018/Active"
$TargetFolderURL = "Shared Documents"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#sharepoint online powershell move folder
Move-PnPFolder -Folder $SourceFolderURL -TargetFolder $TargetFolderURL
This moves the folder “Active” and its contents to the same library’s root. You can use this cmdlet to move a folder to another folder or another library within the same site!
SharePoint Online: Move a folder to another site using PowerShell
If you want to move a folder to a different site collection, use the Move-PnPFile cmdlet instead, which supports moving folders across site collections.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$SourceFolderURL= "Branding/Logos" #Site Relative URL from the current site
$TargetFolderURL = "/sites/Purchase/Shared Documents" #Server Relative URL of the Target Parent Folder
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#move folder between sites in sharepoint online using powershell
Move-PnPFile -SiteRelativeUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Force -AllowSchemaMismatch
The source folder and all its contents, including subfolders and files, will be moved to the new location.
Wrapping up
Moving a folder in SharePoint Online is a handy feature that can help you organize and manage your content. With these simple steps, you can easily move a folder using the user interface, drag and drop or PowerShell script. Note that when you move a folder in SharePoint Online, any links or references to the folder may be affected. If you have links or references to the folder, make sure to update them after moving the folder to ensure that they point to the correct location.
Using the PNP Powershell method, is there a limitation to the number of files moved?
Thanks for this. How would I move or copy files/folders from one sharepoint to another, such as from an old on-premise sharepoint to a new sharepoint online location?
Well, You need to use SharePoint Migration Tool’s PowerShell cmdlets – Either from Microsoft or ShareGate!
I am receiving File NOT Found error message when i try to move Sharepoint folder to a library in a different site collection. is this is a limitation or i am i missing something? Can someone help me on this?
Use the PnP PowerShell method.
I am getting the below error message. please advice
—————————————————————–
PS C:\Users\Administrator\Downloads> .copyfolder.ps1
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Error Moving the Folder! Exception calling “ExecuteQuery” with “0” argument(s):
“The destination file already exists.”
——————————————————————
As the error message says, The destination folder already exists, Set $MoveCopyOpt.KeepBoth = $True to move the folder to destination – SharePoint automatically renames the newly moved folder!