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
Use this PowerShell script to import all files and folders from a network path to 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,"").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,"").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="http://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

To migrate network file share to SharePoint Online, use: How to Migrate File Share to SharePoint Online using PowerShell?

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.