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 a folder in SharePoint Online modern experience is quite simple!

  • Navigate to your SharePoint Online library >> Select the Folder to move
  • 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 full list of sites that you can move to.
    sharepoint online move folder powershell
  • You can also create a new folder in the destination. Click on the “Move here” button to start moving the folder.
    sharepoint online move a folder

This moves the folder with all its files to the selected destination. Please note, the “Move To” is not available in SharePoint Online classic experience.

SharePoint Online: Move folder using PowerShell

We can also use PowerShell to move a folder in SharePoint Online. Let’s move the folder up a level.

#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

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!

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 to the same library in SharePoint Online.

#Config Variables
$SiteURL = "https://crescenttech.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” to the same library’s root. You can use this cmdlet to move a folder to another folder, move a folder to another library!

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!

3 thoughts on “SharePoint Online: How to Move a Folder using PowerShell?

  • January 13, 2020 at 7:19 PM

    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?

    Reply
  • March 16, 2019 at 6:11 PM

    I am getting the below error message. please advice

    —————————————————————–
    PS C:UsersAdministratorDownloads> .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.”
    ——————————————————————

    Reply
    • March 18, 2019 at 9:42 PM

      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!

      Reply

Leave a Reply