I've managed to get to the point where the download should happen but no luck. I know its something to do with how I am using the stream/writer. Img2: File uploaded to SharePoint document library. Img3: File downloaded to given local path. This solution is brought to you by our SharePoint. It will work from any client PC as long as you have permissions to access the file. Why not Office 365? O365 requires an authentication cookie. (When I get some.
Download all files from SharePoint Online
Update (2020-01-28): I have updated this script to include the -Force parameter for Get-PnpFile, allowing you to willy-nilly overwrite destination files.
Yesterday, I came across a request for a script or method to download all files from SharePoint Online.
At first, I balked because CSOM isn’t my favorite and kind of finicky, but then remembered that we’d released the SharePoint PnP PowerShell cmdlets a while back. So, I decided to give those a peek and see what was in there, and was delighted to discover a few cmdlets–namely, Find-PnpFile and Get-PnpFile.
And thus my mini-project began.
After testing them for a while, I did find that they don’t have quite the flexibility for locating files. In CSOM, we can use a CAML query (although I’ve had mixed results with it and generally perform some regular expression filtering/matching afterwards). For example, perhaps I want to do something with all of the .MP3 files in a given site:
[regex]$Pattern = ‘\.mp3$’
$CamlQuery.ViewXml =“<View Scope=’RecursiveAll’ />”
#write-host “File $($obj[“FileRef”]) matches.
It’s like a ton of code to do something.
Find-PnpFile seems like it has much easier syntax:
Find-PnpFile -Match <string>
That’s it. Unfortunately, string is literally just that–a single string (not a match in the regular expression kind of way). So, we can’t do multiple file filters or anything with it, which makes it harder to include or exclude things.
That being said, I plowed on gave it the old college try. I don’t know how it will scale for sites or document libraries with hundreds of thousands of files, but I guess that’s what the user community is for. 🙂
I pass the values submitted for List, Folder, and Match to Find-PnpFile. The only one that’s required is Match, and if you don’t put anything in, the script assumes you want everything. Everything will include all of the HTML, CSS, and everything else in the site. Find-PnpFile also throws errors if you don’t have a matching folder or list (if you specify a name), and then seems to not honor the Match parameter values, so YMMV.
At any rate, go ahead and give it a whirl–let me know what you think!