Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

48
Download all files from SharePoint site locally
Post Flair (click to view more posts with a particular flair)
Post Body

Thanks to u/TheB4ber for opening my eyes to Connect-PnPOnline.

This script will use your current credentials to access the desired SP site and download all files locally.

Feel free to improve this script as I wrote it quickly.

function downloadFilesFromSp(){

[CmdletBinding()]
param(
      [Parameter(Position=0,mandatory=$true)]
      [string]$spVer,
      [string]$url, 
      [string]$driveLtr,
      [string]$localPath)

BEGIN{

    #If Module Does Not Exist Then Install based on provided version
    if (!(Get-Module -Name ('SharePointPnPPowerShell'   $spVer))) {
        switch($spVer){
                   "2013"{
                   Install-Module SharePointPnPPowerShell2013
                   }
                   "2016"{
                   Install-Module SharePointPnPPowerShell2016
                   }
                   "2019"{
                   Install-Module SharePointPnPPowerShell2019
                   }
                   "SPO"{
                   Install-Module SharePointPnPPowerShellOnline
                   }
               }
        } 

       #Create PSDrive
       Connect-PnPOnline -Url $url -CurrentCredentials -CreateDrive -DriveName $driveLtr 

       #Get All Items Recursively

       #$siteItems =  (gci ($driveLtr   ':'))
       $siteItems =  (gci ($driveLtr   ':')) | ?{$_.GetType().Name -eq 'Folder'} )

       #Separate Folders and Files
       #$folders = $siteItems | ?{$_.GetType().Name -eq 'Folder'}
       #$files = $siteItems | ?{$_.GetType().Name -eq 'File'}

    }
PROCESS{
        #Create Folder Start
        #$folders | ForEach-Object { New-Item -ItemType directory -path ($localPath   $_.ServerRelativeUrl) -force }


        #Copy Files Start
        #$files | ForEach-Object { Copy-Item ('Z:'   $_.ServerRelativeUrl) -Destination ($localPath   ($_.ServerRelativeUrl -replace $_.Name)) } 


        #Create Root Directory            
        New-Item -ItemType directory -path ($localPath   (Get-PSDrive $driveLtr).CurrentLocation) -force | Out-Null

        $siteItems | ForEach-Object { 
                Copy-Item ((Get-PSDrive $driveLtr).Name   ":\"   (Get-PSDrive $driveLtr).CurrentLocation   "\"   $_.Name) -Destination ($localPath   (Get-PSDrive $driveLtr).CurrentLocation) -recurse -container
                }

    }
END{
    #Remove PSDrive
    Get-PSDrive $driveLtr | Remove-PSDrive
    }
}

downloadFilesFromSp  -spVer 2016 -url 'https://sharepoint.contosos.com/{site}' -driveLtr 'Z' -localPath 'C:/temp'

Edit:

I optimized the code but I’m sure there’s more that can be done. This should be faster. I also left my old code in but commented out in case you want to use the more broken down method.

Obligatory I am not responsible for how you choose to use this I just wanted to provide a method to quickly backup data from a SharePoint site

Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
26,663
Link Karma
8,214
Comment Karma
18,430
Profile updated: 1 week ago
Posts updated: 8 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
4 years ago