Office 365 SharePoint Online Move Folder To Other Library Using Power Shell

This blog will move all files from one folder to another folder in document library in SharePoint Online using Powershell Cmdlts.

Creating folder in a SharePoint document library gives you an efficient way to group and manage your content, such as grouping reports by fiscal year or department.

Move Folder in SharePoint 2013 Online UI method.
  • Select the items you want to move.
  • In the main menu at the top of the page, click Move to. If you don't see Move to, click the ellipses (...) on the main menu, and then click Move to
Steps
  • Open SharePoint Management Shell.
  • Copy the below Code and paste it.
  • Run the Code. 
  1. ##########################    
  2.   
  3. #Script  : Below Script will move files from one folder to other  Folder in Office 365 SharePoint Online   
  4. #Author  : Gowtham Rajamanickam    
  5. #Date    :16-03-2017  
  6. #Version:1.0    
  7.   
  8. #########################  
  9.  
  10. #Before Run the Scritp you must load the Client Assembly files   
  11.    
  12. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  13. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  14.      
  15.   
  16. function createFolder{  
  17.    
  18. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url)  
  19. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecurePassword)  
  20. $ctx.credentials = $credentials  
  21.  
  22. #Load items  
  23. $list = $ctx.Web.Lists.GetByTitle($listTitle)  
  24. $query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()  
  25. $query.FolderServerRelativeUrl=$sourceFolder;  
  26. $items = $list.GetItems($query)  
  27. $ctx.Load($items)  
  28. $ctx.ExecuteQuery()  
  29.  
  30.  
  31. #Move file(s)  
  32. foreach ($item in $items){  
  33.   
  34.   if($item.FileSystemObjectType -eq [Microsoft.SharePoint.Client.FileSystemObjectType ]::File) {   
  35.   
  36.      $destFileUrl = $item["FileRef"].ToString().Replace($sourceFolder,$destFolder)  
  37.      $item.File.MoveTo($destFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)  
  38.      $ctx.ExecuteQuery()  
  39.   }  
  40. }  
  41.   
  42. $listTitle = "TutorialDocuments"  
  43. $sourceFolder = "/Documents/2016"  
  44. $destFolder = "/Documents/2017"  
  45.   
  46. createFolder  
Was my Blog helpful?Was it missing content? If so, please let us know what's confusing or missing at the bottom of this page.

Thanks for reading my blogs.