Migrate files with folder structure to SharePoint 2013 document library using PowerShell

PowerShell to Migrate files & folder structure in SharePoint
InfoPath alternatives for form designing SharePoint

Objective:
As part of migration we get request from end user to migrate their data from various places. In this article I would like take one of the scenario to upload files from local drive / network share with the same folder structure in to SharePoint.
Local Folder structure:

PowerShell to Migrate files & folder structure in SharePoint
PowerShell to Migrate files & folder structure in SharePoint

PowerShell Script to migrate document with folder structure:



try {
#Referencing to SharePoint PowerShell
Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue
Start-SPAssignment -Global
#Recursive Function to read through all the files and folders till the leaf node
function Recurse([string]$path, $web, $docLibrary,$drive) {
$objFStream = new-object -com scripting.filesystemobject
#Fetching the current folder name in the network drive path
$folder = $objFStream.getfolder($path)
$fldPath = $path.Replace($drive, ”).Replace(‘\’, ‘/’)
#I am removing the current folder name to create destination folder path to upload the files.
$desPath = ($fldPath + ‘$$’).Replace($folder.Name + ‘$$’, ”)
Write-Host “Exporting data from folder :: “$folder.Name
#Validating for the existence of the folder, if the folder is not existed in Library I am creating the folder in document library
$objFldVald = $web.GetFolder($docLibrary.RootFolder.ServerRelativeUrl + $fldPath);
if ($objFldVald.Exists -eq $false) {
$docFldCreate = $docLibrary.Items.Add($docLibrary.RootFolder.ServerRelativeUrl.TrimEnd() + $desPath.TrimEnd(‘/’), [Microsoft.SharePoint.SPFileSystemObjectType]”Folder”, $folder.Name);
$docFldCreate.Update()
}
#Reading all the files in the current folder
foreach ($objFile in $folder.files) {
#Create file stream object from file
$fileStream = ([System.IO.FileInfo] (Get-Item $objFile.Path)).OpenRead()
$contents = new-object byte[] $fileStream.Length
$fileStreamLength = $fileStream.Read($contents, 0, [int]$fileStream.Length);
$fileStream.Close();
#Add file to document library in the same folder as in Network drive.
$DocLibFld = $web.getfolder($docLibrary.RootFolder.ServerRelativeUrl.TrimEnd() + $fldPath)
[Microsoft.SharePoint.SPFile]$spFile = $DocLibFld.Files.Add($DocLibFld.Url + “/” + $objFile.Name, $contents, $true)
Write-Host “File =” $objFile.Name “`tUploaded to ” $web.Url”/”$DocLibFld
}
#Checking if there any sub folders in the current folder if there any I am calling the Rucrse loop to process the folder.
foreach ($objSubFld in $folder.subfolders) {
Recurse $objSubFld.path $web $docLibrary $drive
}
}
#Enter the site URL.
$webUrl = “http://sharepoint13:12345/”
$web = Get-SPWeb -Identity $webUrl
#Mention the document library name in which the files need to be uploaded.
$docLibrary = $web.Lists[“Documents”]
#Provide the path of the local folder to be migrated to SharePoint.
Recurse “S:\Export Data” $web $docLibrary ‘S:’
Write-Host “Migration Completed Sucessfully …!!!”
$web.Close()
Stop-SPAssignment -Global
}
catch {
Write-Host “`n Error:: $($_.Exception.Message)” -ForegroundColor Red -BackgroundColor Yellow
}



The power of this script is:
– It can upload any type of files and I have tested with
• Word Documents
• Excel Files
• PDF
• PPT documents
• Microsoft Project Planner documents
• Image files
• Text files

– No limit on depth of the folder
– No limit on number of files in folder to be uploaded
– NO HARD CODING OF ANY LIMITS / DOCUMENT TYPES
The output for the above script:

PowerShell to Migrate files & folder structure in SharePoint tutorial
PowerShell to Migrate files & folder structure in SharePoint tutorial

Hope this helps in migration, Enjoy SharePoint Migration friends!!!

Similar SharePoint 2013 Tutorials

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →

Leave a Reply