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" } }
Deleted
May 21, 2024NicolasKheirallah Thank you for your suggestion! In the script you've sent, I see that script is using a copy option. Is this also possible for moving? Or not?
I'm interested in this archiving solution, but how does this work exactly? Aside from that script? I mean, if any data is ever needed, how can this data be approached in userfriendly way? As in, would something like be also do-able for an end-user, or is this more a admin job?
If you have any more tips/tricks how to start setting up this SharePoint archiving solution, I'm really looking forward to your reaction!
Thanks in advance!
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"
}
}
- DeletedJun 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! 😄