Sharepoint - Moving files between libraries

Hafiz Hussain 1 Reputation point
2022-12-06T04:27:00.687+00:00

Hi im trying to move a large number of files between sharepoint libraries, but keep running into this issue where it says.
Can someone advice what did i do wrong here..

Move-PnPFile : Parameter set cannot be resolved using the specified named parameters.

My code is as followed

$SiteURL = "https://XXXXX.sharepoint.com/sites/XXXXXX"
$SourceLibraryURL = "Shared Documents/XXXXXX" #Site Relative URL from the current site
$TargetLibraryURL = "/sites/XXXXXXX/Shared%20Documents/XXXXXXX" #Server Relative URL of the Target Folder

Connect to PnP Online

Connect-PnPOnline -Url $SiteURL

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
}

Microsoft 365 and Office SharePoint For business Windows
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-12-06T07:19:11.687+00:00

    Hi @Hafiz Hussain ,

    According to my research and testing, you can try to use the following code to move files between libraries:

    #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"  
        
    #powershell script to move files in sharepoint online - Function  
    Function Move-SPOFile([String]$SiteURL, [String]$SourceFileURL, [String]$TargetFileURL)  
    {  
        Try{  
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
            
            #sharepoint online powershell to move files  
            $MoveCopyOpt = New-Object Microsoft.SharePoint.Client.MoveCopyOptions  
            $Overwrite = $True  
            [Microsoft.SharePoint.Client.MoveCopyUtil]::MoveFile($Ctx, $SourceFileURL, $TargetFileURL, $Overwrite, $MoveCopyOpt)  
            $Ctx.ExecuteQuery()  
        
            Write-host -f Green "File Moved Successfully!"  
        }  
        Catch {  
        write-host -f Red "Error Moving the File!" $_.Exception.Message  
        }  
    }  
        
    #Set Config Parameters  
    $SiteURL="https://xxx.sharepoint.com/sites/zellatest"  
    $SourceFileURL="https://xxxx.sharepoint.com/sites/zellatest/upload/theme.docx"  
    $TargetFileURL="https://xxxx.sharepoint.com/sites/zellatest/test1/theme.docx"  
        
    #Get Credentials to connect  
    $Cred= Get-Credential  
        
    #Call the function to Move the File  
    Move-SPOFile $SiteURL $SourceFileURL $TargetFileURL  
    

    My test result:
    267557-image.png

    More information for reference: SharePoint Online: Move a File between Document Libraries using PowerShell

    Hope it can help you. Thanks for your understanding.

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.