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):
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.
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.
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.
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
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.