Forum Discussion
Deleted
May 16, 2024Copy/move folders from SharePoint site to OneDrive user using Powershell
Hi community members! Hope you are doing allright! I'm hoping one of you is willing to help me out a bit. I will try to describe my case as clear as possible: A customer of mine is using an a...
- May 21, 2024
Deleted
Yeah! just change from Copy-PnPFile to Move-PnPFile.
You can also make the code more efficient using Move-PnPFolder. But it doesn't really like large folders in my experience
# Connect to the source site collection $sourceSiteUrl = "https://yourtenant.sharepoint.com/sites/SourceSiteCollection" Connect-PnPOnline -Url $sourceSiteUrl -Interactive # Connect to the target site collection $targetSiteUrl = "https://yourtenant.sharepoint.com/sites/TargetSiteCollection" Connect-PnPOnline -Url $targetSiteUrl -Interactive # Define source and target library and folder paths $sourceLibrary = "Documents" $targetLibrary = "Documents" # Get all items from the source library $sourceItems = Get-PnPListItem -List $sourceLibrary -PageSize 2000 foreach ($item in $sourceItems) { $sourceFileUrl = $item.FieldValues["FileRef"] $targetFileUrl = $sourceFileUrl -replace "$sourceLibrary", $targetLibrary $targetFileUrl = $targetFileUrl -replace $sourceSiteUrl, $targetSiteUrl try { Move-PnPFile -SourceUrl $sourceFileUrl -TargetUrl $targetFileUrl -Force -OverwriteIfAlreadyExists Write-Host "Moved: $sourceFileUrl to $targetFileUrl" } catch { Write-Host "Failed to move: $sourceFileUrl" } }
May 21, 2024
Deleted
Yeah! just change from Copy-PnPFile to Move-PnPFile.
You can also make the code more efficient using Move-PnPFolder. But it doesn't really like large folders in my experience
# Connect to the source site collection
$sourceSiteUrl = "https://yourtenant.sharepoint.com/sites/SourceSiteCollection"
Connect-PnPOnline -Url $sourceSiteUrl -Interactive
# Connect to the target site collection
$targetSiteUrl = "https://yourtenant.sharepoint.com/sites/TargetSiteCollection"
Connect-PnPOnline -Url $targetSiteUrl -Interactive
# Define source and target library and folder paths
$sourceLibrary = "Documents"
$targetLibrary = "Documents"
# Get all items from the source library
$sourceItems = Get-PnPListItem -List $sourceLibrary -PageSize 2000
foreach ($item in $sourceItems) {
$sourceFileUrl = $item.FieldValues["FileRef"]
$targetFileUrl = $sourceFileUrl -replace "$sourceLibrary", $targetLibrary
$targetFileUrl = $targetFileUrl -replace $sourceSiteUrl, $targetSiteUrl
try {
Move-PnPFile -SourceUrl $sourceFileUrl -TargetUrl $targetFileUrl -Force -OverwriteIfAlreadyExists
Write-Host "Moved: $sourceFileUrl to $targetFileUrl"
} catch {
Write-Host "Failed to move: $sourceFileUrl"
}
}
Deleted
Jun 18, 2024
Forget to reach out back to you soon, but I was able to make it work with the answer(s) you replied. Thank you for your suggestions!
- Jun 19, 2024Great that it worked out! 😄