Author Posts

February 17, 2016 at 6:32 am

We currently use a 3rd party software to backup our SP document libraries locally to a NAS – it does so through a mapped drive (WebDAV) – this is problematic for many reasons.

Should I be able to create a powershell script that connect to a document library and copies the files locally? Then on subsequent runs, copies only file that changed? I'm looking to have a sync'd copy locally of the SP document library.

I know how to connect to the SP Online site with Powershell, but once connected then what? Can I use robocopy to then copy the documents? If so, how does robocopy address the doc. library? by URL once connected?

I appreciate any thoughts on the topic.

Thank you

February 17, 2016 at 7:32 am

Have you looked at Robocopy? Comes with Windows and will save a lot of reinventing the wheel.

February 17, 2016 at 12:55 pm

Yes, I was thinking I could use Robocopy once connected. Wondering if anyone has done it before with SP Online doc libraries and aware of any gotchas. I'll try to hack my way through it this weekend and see what I can come up with.

February 18, 2016 at 3:37 am

I would use the OfficeDevPnP PowerShell Cmdlets and make use of the Get-SPOFile Cmdlet to do this within a foreach loop with an if statement in there as well.

Example would be

$downloadLocation = 'C:\DownloadLocation'
$date = (get-date).AddDays(-1)
$ctx = Get-SPOContext
$list = Get-SPOList -Identity "ListName"
$list.RootFolder.Files
$files = $list.RootFolder.Files
$ctx.Load($files)`
$ctx.ExecuteQuery()

foreach ($file in $files) {
    if ($file.TimeLastModified -gt $date) { 
           Get-SPOFile -ServerRelativeUrl $file.ServerRelativeUrl -Path $DownloadLocation -Filename $file.Name }
}

That will do what your looking for and the OfficeDevPnP Cmdlets can be downloaded from the PowerShell Galllery with the following Command

Install-Module OfficeDevPnP.PowerShell.V16.Commands

or from Github directly https://github.com/OfficeDev/PnP-PowerShell

Hopefully this will be useful for you

April 18, 2017 at 5:58 pm

I know this is an old post, but I ran into some errors when I tried to implement it. I've gotten to the point where I can download all files in the root folder. However, I'd like to be able to download all files in the root folder and all subfolders. I'm hoping you guys might have some pointers. Any help would be appreciated.

$downloadLocation = 'C:\DownloadLocation'
$date = (get-date).AddDays(-1)
$ctx = Get-SPOContext
$files = (Get-PNPList -Identity "ListName").RootFolder.Folders
$ctx.Load($files)`
$ctx.ExecuteQuery()
foreach ($file in $files) {
if ($file.TimeLastModified -gt $date) {
Get-PNPFile -ServerRelativeUrl $file.ServerRelativeUrl -Path $DownloadLocation -Filename $file.Name -AsFile }
}

(Note that I went to the GitHub Repository and followed the directions there to load the module.)

April 20, 2017 at 2:53 pm

Edit: I don't know how to edit my post, so I'm just double posting.

I know this is an old post, but I ran into some errors when I tried to implement it. I've gotten to the point where I can download all files in the root folder. However, I'd like to be able to download all files in the root folder and all subfolders. I'm hoping you guys might have some pointers. Any help would be appreciated.

$downloadLocation = 'C:\DownloadLocation'
$date = (get-date).AddDays(-1)
$ctx = Get-SPOContext
$files = (Get-PNPList -Identity "ListName").RootFolder.Files
$ctx.Load($files)`
$ctx.ExecuteQuery()
foreach ($file in $files) {
if ($file.TimeLastModified -gt $date) {
Get-PNPFile -ServerRelativeUrl $file.ServerRelativeUrl -Path $DownloadLocation -Filename $file.Name -AsFile }
}

Thanks again!