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.
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
Subreddit
Post Details
- Posted
- 4 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/PowerShell/...