I not too long ago had a requirement to maneuver all of the contents of a doc library to a different library and anyone that has had the displeasure of getting to maneuver information in SharePoint is aware of what a ache that may be.
To make issues worse, as a result of particular setup I used to be coping with the ‘Content material & Construction’ device was a non-option because it wasn’t functioning accurately and there have been a lot of nested folders within the supply library. Fortunately this gave me the prospect to make use of one in all my favorite energy instruments – PowerShell.
This text incorporates a script which can copy all contents of a single library to a different library, by utilizing the built-in MoveTo command the place potential so no knowledge is misplaced or by copying if the supply and goal libraries are in several website collections.
PowerShell Script
Directions
- This must be run on the SharePoint server, working it remotely would require modification.
- Working consumer ought to ideally have admin entry, however full entry to each libraries and the server must be sufficient.
- Replace the ‘Static Variables’ –
- SourceWebURL
- SourceLibraryTitle
- DestinationWebUrl
- DestinationLibraryTitle
Script
$ver = $host | choose model if($Ver.model.main -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"} if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) { Write-Progress -Exercise "Loading Modules" -Standing "Loading Microsoft.SharePoint.PowerShell" Add-PSSnapin Microsoft.SharePoint.PowerShell } $ErrorActionPreference = "Cease" #Set Static Variables $SourceWebURL = "https://sharepoint.area/website" #The URL of the supply web site $SourceLibraryTitle = "libraryTitle" # The title of the supply record $DestinationWebURL = "https://sharepoint.area/website" #The URL of the vacation spot web site $DestinationLibraryTitle = "libraryTitle" #The title of the vacation spot record ######################### Collect related particulars ################################################### #Retrieve the supply net and record $sWeb = Get-SPWeb $SourceWebURL $sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle} if($sList -eq $null) { throw "Did not discover supply record" } #Retrieve the vacation spot net and record $dWeb = Get-SPWeb $DestinationWebURL $dList = $dWeb.Lists | ? {$_.title -like $DestinationLibraryTitle} if($dList -eq $null) { throw "Did not discover goal record" } #Retrieve all folders within the supply record. $AllFolders = $sList.Folders $allItems = $sList.Gadgets # Examine whether or not Examine-In enabled heading in the right direction library $checkinEnabled = $dList.ForceCheckout ################### Create all folders in new location ############################################## $sortedFolders = $AllFolders.Folder | type $_.Url # Loop by way of the whole record of folders $progressCount = 0 foreach($Folder in $sortedFolders) { # Simply writes a pleasant progress report Write-Progress -Id 0 -Exercise "Creating folders in goal listing" -Standing "$progressCount of $($sortedFolders.Rely)" -PercentComplete (($progressCount / $sortedFolders.Rely) * 100) # Outline the brand new location of the folder $parentUrl = $Folder.ParentFolder.Url -replace $sList.RootFolder.Title, $dList.RootFolder.Title $newLocation = $Folder.Url -replace $sList.RootFolder.Title, $dList.RootFolder.Title #If an identical folder is not discovered within the new library then create it if(!($dList.Folders | ? {$_.URL -eq $newLocation})) { $newFolder = $dList.AddItem(($DestinationWebURL + "/" + $parentUrl), [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.Title) $newFolder["Title"] = $Folder.Title $newFolder.Replace() } $progressCount++ } # Stops the progress report Write-Progress -Id 0 -Exercise " " -Standing " " -Accomplished #################### Transfer information if in similar net ###################################################### # If in the identical location then transfer the paperwork $progressCount = 0 if($SourceWebURL -eq $DestinatioNWebURL) { #Transfer all the paperwork $depend = $allItems.Rely - 1 #Loop by way of every of the paperwork found within the root of the library and transfer any gadgets discovered whereas($depend -ne 0) { # Simply writes a pleasant progress report Write-Progress -Id 0 -Exercise "Transferring paperwork to focus on listing" -Standing "$progressCount of $($allItems.Rely)" -PercentComplete (($progressCount / $allItems.Rely) * 100) # Work out the brand new vacation spot then transfer the file $itemNewUrl = $allItems[$count].Url -replace $sList.RootFolder.Title, $dList.RootFolder.Title $Dest = $DestinationWebURL + "/" + $itemNewUrl $allItems[$count].File.moveTo($Dest, $true) $depend-- $progressCOunt++ } } # Stops the progress report Write-Progress -Id 0 -Exercise " " -Standing " " -Accomplished ###################### Copy information if completely different net ################################################# # If the supply and vacation spot are completely different websites then want to repeat the merchandise over $progressCount = 0 if($SourceWebURL -ne $DestinationWebURL) { #Loop by way of every of the paperwork found within the root of the library and carry out some motion foreach($Merchandise in $AllItems) { # Simply writes a pleasant progress report Write-Progress -Id 0 -Exercise "Copyring paperwork to focus on listing" -Standing "$progressCount of $($AllItems.Rely)" -PercentComplete (($progressCount / $AllItems.Rely) * 100) #Get the Binary Stream of the referenced file, such that we will create the identical file within the vacation spot surroundings $sBytes = $Merchandise.File.OpenBinary() #Create A New file utilizing the title and binary stream from the unique doc, assign it to a variable. This variable will later be referred to as when setting properties $dFile = $dList.RootFolder.Recordsdata.Add($Merchandise.Title, $sBytes, $true) #Return all fields for the supply merchandise which aren't read-only $AllFields = $Merchandise.Merchandise.Fields | ? {!($_.sealed)} #Loop by way of all the fields returned and carry out some motion foreach($Discipline in $AllFields) { #If the supply merchandise has a property that matches the title of the sphere carry out some motion if($Merchandise.Properties[$Field.Title]) { #If the vacation spot file doesn't include the identical property because the supply merchandise carry out some motion. if(!($dFile.Properties[$Field.title])) { #Add a property to the file matching title and worth of the identical area within the authentic merchandise $dFile.AddProperty($Discipline.Title, $Merchandise.Properties[$Field.Title]) } else { #If the property already exists, set the worth on the vacation spot merchandise to the identical worth because it was within the supply merchandise. $dFile.Properties[$Field.Title] = $Merchandise.Properties[$Field.Title] } } } #Commit the modifications by updating the vacation spot file. $dFile.Replace() if($checkinEnabled -and $merchandise.File.CheckOutStatus -ne "None") { $merchandise.File.CheckIn("Checked In after scripts Copy/Transfer") } } } # Stops the progress report Write-Progress -Id 0 -Exercise " " -Standing " " -Accomplished
I wish to thank Roger Cormier for his article ‘How to Copy SharePoint Documents Between Site Collections Using PowerShell‘ which supplied foundation for this script.