Migrate Network File Share to SharePoint using PowerShell
Requirement: Migrate Network File Share to SharePoint using PowerShell.
PowerShell Script to Import Network File Share into SharePoint
Migrating your network file shares to SharePoint doesn’t have to be complicated. With PowerShell, you can quickly and easily migrate your files to SharePoint folders. This post will show you how to migrate a network file share to SharePoint using PowerShell.
Use this PowerShell script to import all files and folders from a network path to a SharePoint document library:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to migrate all Files and Folders from FileShare to SharePoint
Function Migrate-FileShareToSharePoint()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $TargetLibraryName,
[Parameter(Mandatory=$true)] [string] $SourceFolderPath,
[Parameter(Mandatory=$false)] [bool] $Overwrite = $False
)
#Get the Target Folder to Upload
$Web = Get-SPWeb $SiteURL
$TargetLibrary = $Web.Lists[$TargetLibraryName]
$TargetFolder= $TargetLibrary.RootFolder
#Get All Files and Folders from the Source
Get-ChildItem $SourceFolderPath -Recurse | ForEach-Object {
#Get Source Item's metadata
$CreatedDate= [DateTime]$_.CreationTime
$ModifiedDate = [DateTime]$_.LastWriteTime
If ($_.PSIsContainer -eq $True) #If its a Folder!
{
#Frame Target Folder URL
$TargetFolderRelativeURL = $TargetFolder.ServerRelativeURL+$_.FullName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
#$Subfolder = $Web.GetFolder($TargetFolder.URL + "/" + $_.Name)
$FolderToCreate = $Web.GetFolder($TargetFolderRelativeURL)
Write-host -f Yellow "Ensuring Folder '$TargetFolderRelativeURL'" -NoNewline
#Ensure Target Folder Exists
If ($FolderToCreate.Exists -eq $false)
{
#Create New Sub-Folder
$FolderToCreate=$Web.Folders.Add($TargetFolderRelativeURL)
$FolderToCreate.Item["Created"] = $CreatedDate
$FolderToCreate.Item["Modified"] = $ModifiedDate
$FolderToCreate.Item.Update()
Write-host -f Green "`tCreated Folder!"
}
Else
{
Write-host -f Green "`tFolder Already Exists!"
}
}
Else #If its a File
{
$FolderToUpload = $TargetFolder.ServerRelativeURL + $_.DirectoryName.Replace($SourceFolderPath,[string]::Empty).Replace("\","/")
$TargetFileURL = $FolderToUpload+"/"+$_.Name
$SourceFilePath = $_.FullName
Write-host -f Yellow "Ensuring File '$TargetFileURL'" -NoNewline
#Check if file exists
$FileToUpload = $Web.GetFile($TargetFileURL)
If($FileToUpload.Exists -and $Overwrite -eq $False)
{
Write-host -f Green "`tFile Already Exists!"
}
Else
{
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $SourceFilePath)).OpenRead()
#Upload the File to SharePoint Library's Folder
$ParentFolder = $Web.GetFolder($FolderToUpload)
$FileToUpload = $ParentFolder.Files.Add($_.Name, $FileStream, $TRUE)
#Set Metadata
$FileToUpload.Item["Created"]=$CreatedDate
$FileToUpload.Item["Modified"]=$ModifiedDate
$FileToUpload.Item.Update()
#Close file stream
$FileStream.Close()
Write-host "`tFile Uploaded Successfully!" -ForegroundColor Green
}
}
#Read-Host
}
}
#Set parameter values
$SiteURL="https://intranet.crescent.com/sites/marketing/us"
$TargetLibraryName="Documents"
$SourceFolderPath= "\\Cre-LT575\Salaudeen\Project Documents" #Or any local path: "C:\Temp\Docs"
#Call the function to Upload All files & folders from network Fileshare to SharePoint library
Migrate-FileShareToSharePoint -SiteURL $SiteURL -SourceFolderPath $SourceFolderPath -TargetLibraryName $TargetLibraryName
In conclusion, migrating a network file share to SharePoint using PowerShell is a powerful and efficient way to move large amounts of data from one platform to another. By using PowerShell, you can automate migrating the data, including files and folders, from the network file share to SharePoint. This can save time and resources while ensuring that the data is properly transferred and organized in SharePoint.
To migrate network file share to SharePoint Online, use: How to Migrate File Share to SharePoint Online using PowerShell?
Thank you for this valuable script , I did the same to migrate network file server to One Drive but I want to migrate the permissions as well from file server to one Drive of each owner