|
| 1 | +--- |
| 2 | +plugin: add-to-gallery |
| 3 | +--- |
| 4 | + |
| 5 | +# Download all documents from all document libraries in a site, including version history |
| 6 | + |
| 7 | +## Summary |
| 8 | + |
| 9 | +This PowerShell function will download all documents from all document libraries in a site, including version history. The documents will be saved in a folder structure that matches the library structure. You will have to connect to the site first using Connect-PnPOnline before running this script. |
| 10 | +Save the code to a PSM1 file, then import it into your PowerShell session using Import-Module like in Example01. You can then run the function Download-SharePointFiles to download the documents. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +This screenshot shows the script in action downloading documents from a site. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +This screenshot shows the folder structure created by the script after the documents have been downloaded. |
| 19 | + |
| 20 | + |
| 21 | +# [PnP PowerShell](#tab/pnpps) |
| 22 | + |
| 23 | +```powershell |
| 24 | +
|
| 25 | +<# |
| 26 | +.SYNOPSIS |
| 27 | +Downloads files from all SharePoint document libraries in a tenant to a local directory. Requires PnP PowerShell 2.x and and existing connection to the tenant with Connect-PnPOnline. |
| 28 | +
|
| 29 | +.DESCRIPTION |
| 30 | +This script connects to a SharePoint tenant using PnP PowerShell, iterates through all document libraries, and downloads files to a specified local directory. It can optionally overwrite existing files and download all versions of the files. |
| 31 | +
|
| 32 | +.PARAMETER DownloadPath |
| 33 | +Specifies the local directory where files will be saved. |
| 34 | +
|
| 35 | +.PARAMETER Overwrite |
| 36 | +Indicates whether to overwrite existing files in the local directory. |
| 37 | +
|
| 38 | +.PARAMETER IncludeVersions |
| 39 | +Specifies whether to download all versions of the files. |
| 40 | +
|
| 41 | +.EXAMPLE |
| 42 | +Download files to a local directory: |
| 43 | +Download-SharePointFiles -DownloadPath "C:\SharePointDownloads" |
| 44 | +
|
| 45 | +.EXAMPLE |
| 46 | +Download files and overwrite existing ones: |
| 47 | +Download-SharePointFiles -DownloadPath "C:\SharePointDownloads" -Overwrite |
| 48 | +
|
| 49 | +.EXAMPLE |
| 50 | +Download files including all versions: |
| 51 | +Download-SharePointFiles -DownloadPath "C:\SharePointDownloads" -IncludeVersions |
| 52 | +
|
| 53 | +.EXAMPLE |
| 54 | +Download files, overwrite existing ones, and include all versions: |
| 55 | +Download-SharePointFiles -DownloadPath "C:\SharePointDownloads" -Overwrite -IncludeVersions |
| 56 | +#> |
| 57 | +function Download-SharePointFiles { |
| 58 | + |
| 59 | + [CmdletBinding()] |
| 60 | + param ( |
| 61 | + [Parameter(Mandatory = $true)] |
| 62 | + [string]$DownloadPath, # The local directory to save files |
| 63 | +
|
| 64 | + [Parameter(Mandatory = $false)] |
| 65 | + [switch]$Overwrite, # Whether to overwrite existing files |
| 66 | +
|
| 67 | + [Parameter(Mandatory = $false)] |
| 68 | + [switch]$IncludeVersions # Whether to download all versions of the files |
| 69 | + ) |
| 70 | +
|
| 71 | + # Ensure there is a connection to SharePoint |
| 72 | + $connection = Get-PnPConnection -ErrorAction Stop |
| 73 | + if ($null -ne $connection) { |
| 74 | + Write-Verbose "You are connected to your tenant: $($connection.TenantUrl)" |
| 75 | + } else { |
| 76 | + throw "Please connect and try again" |
| 77 | + } |
| 78 | +
|
| 79 | + # Ensure the download directory exists |
| 80 | + if (-not (Test-Path -Path $DownloadPath)) { |
| 81 | + New-Item -Path $DownloadPath -ItemType Directory | Out-Null |
| 82 | + } |
| 83 | +
|
| 84 | + # Get Context if -IncludeVersions is specified |
| 85 | + if ($IncludeVersions.IsPresent) { |
| 86 | + $Ctx = Get-PnPContext |
| 87 | + } |
| 88 | +
|
| 89 | + # Get all document libraries in the site |
| 90 | + Write-Verbose "Fetching all document libraries in the site..." |
| 91 | + $Libraries = Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 } # 101 = Document Library |
| 92 | +
|
| 93 | + # Loop through each library and download files |
| 94 | + foreach ($Library in $Libraries) { |
| 95 | + Write-Host "Processing library: $($Library.Title)" -ForegroundColor Yellow |
| 96 | +
|
| 97 | + # Get all files in the library |
| 98 | + $Files = Get-PnPListItem -List $Library.Title -PageSize 1000 -Fields FileLeafRef, FileDirRef | Where-Object { $_.FileSystemObjectType -eq "File" } |
| 99 | +
|
| 100 | + foreach ($File in $Files) { |
| 101 | + $FileUrl = $File["FileRef"] |
| 102 | + $LocalPath = Join-Path $DownloadPath ($File["FileDirRef"] -replace "/", "\") # Convert SharePoint folder structure to local paths |
| 103 | + $FileName = $File["FileLeafRef"] |
| 104 | + $LocalFilePath = Join-Path $LocalPath $FileName |
| 105 | +
|
| 106 | + # Create local directory if it doesn't exist |
| 107 | + if (-not (Test-Path -Path $LocalPath)) { |
| 108 | + New-Item -Path $LocalPath -ItemType Directory | Out-Null |
| 109 | + } |
| 110 | +
|
| 111 | + # Download the current version of the file |
| 112 | + if (-not (Test-Path -Path $LocalFilePath) -or $Overwrite.IsPresent) { |
| 113 | + Write-Host "Downloading file: $FileName" -ForegroundColor Green |
| 114 | + Write-Verbose $FileUrl |
| 115 | + Get-PnPFile -Url $FileUrl -Path $LocalPath -FileName $FileName -AsFile -Force |
| 116 | + } else { |
| 117 | + Write-Host "File already exists: $FileName. Skipping..." -ForegroundColor Yellow |
| 118 | + } |
| 119 | +
|
| 120 | + # Optionally download all versions |
| 121 | + # got help from https://www.sharepointdiary.com/2018/06/sharepoint-online-download-all-versions-using-powershell.html |
| 122 | + if ($IncludeVersions.IsPresent) { |
| 123 | + Write-Verbose "Fetching versions for: $FileName" |
| 124 | + Write-Verbose $FileUrl |
| 125 | + $pnpfile = Get-PnPFile -Url $FileUrl |
| 126 | + $Versions = Get-PnPProperty -ClientObject $pnpfile -Property Versions |
| 127 | +
|
| 128 | + if ($Versions.Count -gt 0) { |
| 129 | + foreach ($Version in $Versions) { |
| 130 | + # Construct version filename |
| 131 | + $VersionFileName = "$($LocalPath)\$($FileName)_$($Version.VersionLabel)" |
| 132 | + |
| 133 | + #Get Contents of the File Version |
| 134 | + $VersionStream = $Version.OpenBinaryStream() |
| 135 | + $Ctx.ExecuteQuery() |
| 136 | + |
| 137 | + #Download File version to local disk |
| 138 | + [System.IO.FileStream] $FileStream = [System.IO.File]::Open($VersionFileName,[System.IO.FileMode]::OpenOrCreate) |
| 139 | + $VersionStream.Value.CopyTo($FileStream) |
| 140 | + $FileStream.Close() |
| 141 | + |
| 142 | + Write-Host -f Green "Downloading Version $($Version.VersionLabel) to:" $VersionFileName |
| 143 | + |
| 144 | + } |
| 145 | + } else { |
| 146 | + if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"]) { |
| 147 | + Write-Host "No versions available for file: $FileName" -ForegroundColor Yellow |
| 148 | + } |
| 149 | +
|
| 150 | + |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +
|
| 156 | + Write-Host "Download completed!" -ForegroundColor Cyan |
| 157 | +} |
| 158 | +
|
| 159 | +
|
| 160 | +``` |
| 161 | +[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)] |
| 162 | + |
| 163 | +*** |
| 164 | + |
| 165 | + |
| 166 | +## Source Credit |
| 167 | + |
| 168 | +Sample first appeared on [https://pnp.github.io/cli-microsoft365/sample-scripts/spo/spo-download-all-doclibs/](https://pnp.github.io/cli-microsoft365/sample-scripts/spo/spo-download-all-doclibs/) |
| 169 | + |
| 170 | +## Contributors |
| 171 | + |
| 172 | +| Author(s) | |
| 173 | +|-----------| |
| 174 | +| Todd Klindt (https://www.toddklindt.com/blog) | |
| 175 | + |
| 176 | + |
| 177 | +[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)] |
| 178 | +<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-download-all-doclibs" aria-hidden="true" /> |
0 commit comments