How to copy a shared network directory to sharepoint online

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)

$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)

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)

recurse ImportFiles

} # end function ImportFiles


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?

  2. If PowerShell is a requirement for this - I don’t see anywhere in the code you posted where

    has any values assigned? In addition, when you do your
    $FoldersInDirectory = Get-ChlidItem $Directory
    - why are you not using the


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.