Can I copy an entire directory to sharepoint using powershell?

Welcome Forums General PowerShell Q&A Can I copy an entire directory to sharepoint using powershell?

Viewing 8 reply threads
  • Author
    Posts
    • #193111
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      How can I copy or transfer an entire directory to sharepoint using powershell?

      The idea is to create a script and I can then automated to run every week or whenever.  I would want the directory to retain all sub-directories as they are set up in the local folder/network share.  Any information anyone can provide would be really helpful.  Any sample code snippets would be helpful as well.

      Here is the code I have so far ($Directory contains the path of my local path/network share):

        $FoldersInDirectory = Get-ChildItem $Directory | ? {$_.PsIsContainer -eq $True}
          #upload sub-folder to sharepoint
          Foreach ($Foldername in $FoldersInDirectory)
          {
          $SubDirectories = $Foldername
          copy-item $Foldername.fullname -Destination “Shared Documents\$NewDirectory”

          
          $UploadFoldersInDirectory = $UploadNewDirectory.Folders.Add($SubDirectories)
          $Context.Load($UploadFoldersInDirectory)
          $Context.ExecuteQuery()
          }

      I need the structure of the Directory that I am copying to stay exactly the same.

      Example Local Path/Network Share:

      Main Folder: C:/samplefolder

      -Sub Folder 1

      –Under Folder 1

      —Sample File 1

      —Sample File 2

      —Sample File 3

      -Sub Folder 2

      –Sample File 4

      –Sample File 5

      -Sub Folder 3

      –Under Folder 2

      –Under Folder 3

      —Sample File 6

      –Under Folder 4

      —Sample File 7

    • #193117
      Participant
      Topics: 13
      Replies: 1683
      Points: 2,849
      Helping Hand
      Rank: Community Hero

      Found this example:

      https://sharepoint.stackexchange.com/questions/208425/powershell-script-to-transfer-files-from-local-folder-to-document-library

      If this does not work, please provide the current results, you are not explaining what is or is not working.

    • #193135
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      We currently don’t use a SharePoint server.  Our SharePoint is cloud-hosted.  So, I am not able to use the SP-Web command.  I have gotten as far as copying the main folder on the Documents Library and the first set of sub-folders within the main folder.  I am not sure how to now copy over all the child items within the sub-folders.

    • #193183
      Senior Moderator
      Topics: 5
      Replies: 133
      Points: 706
      Helping Hand
      Rank: Major Contributor

      The -Recurse parameter discussed in Example 3 in the Get-ChildItem documentation should make your life a lot easier. It will include all child items of the target directory and all sub-directories (all the way down, not just the first level) by default. It will only get complicated if you need to exclude certain sub-directories or files.

      As for transferring the files to another system, it might be easiest for you to package the directory structure into a .zip using Compress-Archive. If this is for backup purposes, you can just keep the .zip archives on the server and unpack them if you need them. If it’s for active use, you can just unpack them on the other side after the transfer.

    • #193186
      Participant
      Topics: 11
      Replies: 127
      Points: 723
      Helping Hand
      Rank: Major Contributor

      I use the SharePointPnPPowerShellOnline module for uploading files (and create new folders if they don’t exist) to a SharePoint Online Document Library and it works great.

      Check out: https://github.com/SharePoint/PnP-PowerShell

      MS Docs: https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps

    • #193880
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      I added the -Recurse parameter, but it is not copying the directory with all the corresponding subdirectories and all of the child items of the subdirectories.   What the script is doing is creating the main directory and then dumping all the subdirectories under that main directory (including all the childitems of the subdirectories).  I would prefer that the script copy the entire directory as is (Main Directory – Sub-Directory – Childitems).  My code is below and the recurse Parameter is highlighted in red.

       

      #Retrieve list

      $List = $Context.Web.Lists.GetByTitle($DocLibName)
      $Context.Load($List)
      $Context.ExecuteQuery()

      #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 -Recurse | ? {$_.PsIsContainer -eq $True}
      #upload sub-folder to sharepoint
      Foreach ($Foldername in $FoldersInDirectory)
      {
      $SubDirectories = $Foldername
      copy-item $Foldername.fullname -Destination “Shared Documents\$NewDirectory”

      $UploadFoldersInDirectory = $UploadNewDirectory.Folders.Add($SubDirectories)
      $Context.Load($UploadFoldersInDirectory)
      $Context.ExecuteQuery()
      }

    • #193892
      Senior Moderator
      Topics: 5
      Replies: 133
      Points: 706
      Helping Hand
      Rank: Major Contributor

      I added the -Recurse parameter, but it is not copying the directory with all the corresponding subdirectories and all of the child items of the subdirectories. What the script is doing is creating the main directory and then dumping all the subdirectories under that main directory (including all the childitems of the subdirectories). I would prefer that the script copy the entire directory as is (Main Directory – Sub-Directory – Childitems). My code is below and the recurse Parameter is highlighted in red.

      Sorry, yes that makes sense, as you are feeding the recursive output from Get-ChildItem into Copy-Item, so it is performing a separate copy action for each discovered item and placing all of them (one at a time) in the destination directory. Instead, you should apply -Recurse to Copy-Item instead, and it should copy the target directory and all contents as a complete file tree to the destination. For this usage, you don’t need to do Get-ChildItem first, as Copy-Item -Recurse will do the job by itself. This should be faster too, as you won’t need the Foreach loop.

      Also, please use the pre tags as described here so that your code will be properly formatted:

      #Retrieve list
      $List = $Context.Web.Lists.GetByTitle($DocLibName)
      $Context.Load($List)
      $Context.ExecuteQuery()
      
      #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 -Recurse | ? {$_.PsIsContainer -eq $True}
      
          #upload sub-folder to sharepoint
          Copy-Item -Path "$Directory" -Destination "Shared Documents\$NewDirectory" -Recurse
      
      #    Foreach ($Foldername in $FoldersInDirectory) {
      #        $SubDirectories = $Foldername
      #        copy-item $Foldername.fullname -Destination "Shared Documents\$NewDirectory"
      #        $UploadFoldersInDirectory = $UploadNewDirectory.Folders.Add($SubDirectories)
      #        $Context.Load($UploadFoldersInDirectory)
      #        $Context.ExecuteQuery()
      #    } # end foreach subdirectory upload
          
      } # end function ImportFiles
    • #196130
      Participant
      Topics: 4
      Replies: 5
      Points: 62
      Rank: Member

      Thank you for all your help.  I apologize that I have not followed up on this until now due to the hectic holiday season.  My current code is below.  The copy-item command works very well when moving a complete directory from server to server.  However, it has not worked for my need – moving a network directory to SharePoint.  Maybe, what I am asking for is not possible (at least that is what I am beginning to think).  I have gotten as far as creating the first layer of the network directory on SharePoint(The main folder is created and the folders and files that are stored in that folder are also created).   But I cannot seem to copy any of the corresponding sub-folders or files over.  If anyone has any tips or additional hints, please let me know.  Otherwise, it might be time to tell my boss that this venture is going nowhere.  Thanks again for all your help so far.

       

      #Retrieve list
      
      $List = $Context.Web.Lists.GetByTitle($DocLibName)
      $Context.Load($List)
      $Context.ExecuteQuery()
      
      
      #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
    • #197231
      Participant
      Topics: 6
      Replies: 109
      Points: 515
      Helping Hand
      Rank: Major Contributor
Viewing 8 reply threads
  • The topic ‘Can I copy an entire directory to sharepoint using powershell?’ is closed to new replies.