Copy Files from FileShare to appropriate document Libraries in SharePoint Online

This topic contains 3 replies, has 2 voices, and was last updated by  MD 1 year ago.

  • Author
  • #70789

    if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
            Add-PSSnapin Microsoft.SharePoint.PowerShell
        #Script settings
    #Intent is to copy multiple Folders/Subfolders/Documents from a FileShare to the appropriate document libraries in SharePoint Online
    #For Ex - FileShare called Store Documents -> copy content -> SharePoint document library called Store Documents
    #For Ex - FileShare called HR Managers-> copy content -> SharePoint document library called HR Managers
    #Below script only tries to do one FileShare at a time can i do it for all FileShares all at once and is there a way to outout the success and failures when succeeded or failed
    #This script only copies Files now and doesn't copy Folders.
        $webUrl = ""
        $docLibraryName = "Store Documents" 
        $docLibraryUrlName = "Store Documents"    # specify your subfolder url here
        $localFolderPath = "C:\Test" #I will input the UNC Path to FileShares
        #Open web and library
        $web = Get-SPWeb $webUrl
        write-host $webUrl
        $docLibrary = $web.Lists[$docLibraryName]
        write-host $docLibrary
        $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
        write-host $files
        ForEach($file in $files)
        write-host $file
            #Open file
            $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
            #Add file
            $folder =  $web.getfolder($docLibraryUrlName)
            write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
            $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name,[System.IO.Stream]$fileStream, $true)
            write-host "Success"
            #Close file stream
            Write "Error: $ $_" >>c:\logfile.txt
        #Dispose web
  • #70804

    Don Jones


    • #70927


      @Don Jones – Maybe I was not clear this script works only for Files within the File Share and doesn't do Folders, can you please assist.
      I have a File Share called \\abc\store123\home and it has below folders

      Store Documents
      HR Documents

      All these needs to be copied to the document libraries of that Store SharePoint Site in o365

      Store Documents (Document Library)
      HR Documents (Document Library)
      Videos (Document Library)

    • #70946


      #I need help as this script only Copies files within the Folder and not the Folder itself

      #Specify tenant admin and site URL
      $User = ""
      $SiteURL = ""
      $Folder = "\\\Store Documents"  #UNC Path where Store Documents is a folder i am trying to copy
      $DocLibName = "Store Documents" #This is the final destination where the Folder/SubFolders/Files should be moved
      #Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
      Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
      Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
      $Password = Read-Host -Prompt "Please enter your password" -AsSecureString
      #Bind to site collection
      $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
      $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
      $Context.Credentials = $Creds
      #Retrieve list
      $List = $Context.Web.Lists.GetByTitle($DocLibName)
      #Upload file
      #Foreach ($File in (dir $Folder -File))
      Foreach ($File in (Get-ChildItem $Folder -File -Recurse))
      $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
      $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
      $FileCreationInfo.Overwrite = $true
      $FileCreationInfo.ContentStream = $FileStream
      $FileCreationInfo.URL = $File
      $Upload = $List.RootFolder.Files.Add($FileCreationInfo)

You must be logged in to reply to this topic.