How to copy a shared network directory to sharepoint online

Welcome Forums General PowerShell Q&A How to copy a shared network directory to sharepoint online

  • This topic has 2 replies, 3 voices, and was last updated 2 weeks ago by
    Participant
    .
Viewing 2 reply threads
  • Author
    Posts
    • #196718
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      I want to copy an entire network directory to SharePoint online and I all I can seem to capture so far are the main folders and files in the directory.  Any sub-folders or files withing the main folders are not carrying over as intended (The main folders are empty).  My code is below and any assistance is greatly appreciated.

       

      #Upload file
      
      Function ImportFiles()
      {
      
      #Get name of folder
      $NewDirectory = Split-Path $Directory -Leaf
      
      #upload folder to sharepoint
      $UploadNewDirectory = $List.RootFolder.Folders.Add($NewDirectory)
      $Context.Load($UploadNewDirectory)
      $Context.ExecuteQuery()
      
      $FoldersInDirectory = Get-ChildItem $Directory
      
      #upload sub-folder to sharepoint
      
      Foreach ($Item in $FoldersInDirectory)
      {
      if($Item | ? {$_.PsIsContainer -eq $true})
      {
      $Folder = $Item
      $UploadFoldersInDirectory = $UploadNewDirectory.Folders.Add($Folder)
      $Context.Load($UploadFoldersInDirectory)
      $Context.ExecuteQuery()
      }
      
      elseif($Item | ? {$_.PsIsContainer -eq $false})
      {
      $File = $Item
      $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 = $UploadNewDirectory.Files.Add($FileCreationInfo)
      $Context.Load($Upload)
      $Context.ExecuteQuery()
      }
      
      else
      {
      recurse ImportFiles
      }
      }
      
      } # end function ImportFiles
      ImportFiles
    • #197120
      Participant
      Topics: 14
      Replies: 44
      Points: 154
      Helping Hand
      Rank: Participant

      Tomcat,

      I've never done this sort of thing with PowerShell, but I've got some questions.

      1) Is PowerShell the right tool? Doesn't MSFT have a pretty good SharePoint migration tool that you can just point to a directory and it will upload? https://docs.microsoft.com/en-us/sharepointmigration/introducing-the-sharepoint-migration-tool

      2) If PowerShell is a requirement for this – I don't see anywhere in the code you posted where

      $Directory

      or

      $NewDirectory

      has any values assigned? In addition, when you do your

      $FoldersInDirectory = Get-ChlidItem $Directory

      – why are you not using the

      -Recurse

      switch?

      Steve

    • #197228
      Participant
      Topics: 2
      Replies: 16
      Points: 69
      Helping Hand
      Rank: Member

      You may also want to consider a simple RoboCopy. I have done many of these to SharePoint Document Libraries. WebDAV is required to access the SP list, but is typically enabled by default so you should be good to go. RoboCopy will handle your recursion, and if need to do this on a continual basis, robo only copies changed files, not all of them so successive copies will go much faster.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.