unable to move a folder from Site Pages

Aran Billen 906 Reputation points
2025-01-16T10:38:41.04+00:00

Hi everyone,

A staff member has accidentally moved a folder containing many files into the Site Pages section, and we’re unable to find a way to relocate it to another location besides Site Pages without manually saving each file individually, which is very time-consuming.

My question is: is there an easy way to move this folder and its files to another location, such as a document library?

Thank you for your help!

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,230 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AllenXu-MSFT 24,436 Reputation points Microsoft External Staff
    2025-01-17T02:17:27.5033333+00:00

    Hi @Aran Billen,

    Please try this PowerShell script to move folder to another library. Be sure to set the parameters, such as the Site URL and source directory.

    #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://mycompany.sharepoint.com/"
    $FolderSourceURL="https://mycompany.sharepoint.com/sites/sitename/SitePages/foldername"
    $FolderTargetURL="https://mycompany.sharepoint.com/sites/sitename/libraryname/foldername"
     
    #Get Credentials to connect
    $Cred= Get-Credential
     
    #Call the function to Move the Folder
    Move-SPOFolder $SiteURL $FolderSourceURL $FolderTargetURL
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.