Copy OneDrive folder to SharePoint

Modified on Mon, 5 Jan at 5:25 PM

# Install-Module PnP.PowerShell
# Register-PnPEntraIDAppForInteractiveLogin -ApplicationName "PnP.PowerShell" -Tenant [yourtenant].onmicrosoft.com

###########

$SourceOneDriveUrl = "https://contoso-my.sharepoint.com/personal/johnsmith_contoso_com"
$TargetFullUrl = "https://contoso.sharepoint.com/sites/SharePointSiteURL/Subfolder1/Subfolder2"
$ClientID = "4df41441-????-????-????-????????????"

###########

Connect-PnPOnline -Url $SourceOneDriveUrl -ClientId $ClientID

$Items = Get-PnPFolderItem -FolderSiteRelativeUrl "/Documents"

foreach ($Item in $Items) {
    Write-Host "Copying $($Item.Name)..." -ForegroundColor Cyan
    
    try {
        Copy-PnPFile -SourceUrl $Item.ServerRelativeUrl -TargetUrl $TargetFullUrl -Force -ErrorAction Stop
        Write-Host "SUCCESS: $($Item.Name) copied." -ForegroundColor Green
    }
    catch {
        Write-Host "FAILED: Could not copy $($Item.Name). Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article