Skip to content

Commit

Permalink
Merge pull request #748 from petkir/spo-download-files-from-doclib-Ad…
Browse files Browse the repository at this point in the history
…d-Scripts

spo-download-files added  cli-m365-ps
  • Loading branch information
pkbullock authored Oct 7, 2024
2 parents 4393bf5 + d426137 commit de6d7ba
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
51 changes: 51 additions & 0 deletions scripts/spo-download-files-from-doclib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,57 @@ Write-Host "Download completed."
```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
# [CLI for Microsoft 365 using PowerShell](#tab/cli-m365-ps)

```powershell
# Define variables
$siteUrl = "https://yoursharepointsite.sharepoint.com/sites/yoursite"
$libraryPath = "/Shared Documents/YourFolder"
$localDownloadPath = "C:\Downloads\SharePointFiles"
m365 login
function Download-FilesFromSharePoint {
param (
[string]$LibraryPath,
[string]$LocalDownloadPath,
[bool]$Recursive = $true
)
$folder = m365 spo folder get --webUrl $siteUrl --url $LibraryPath --output json | ConvertFrom-Json
if($Recursive){
# Get all files in the folder and subfolders
$files = m365 spo file list --webUrl $siteUrl --folderUrl $LibraryPath --recursive --output json | ConvertFrom-Json
}
else{
# Get all files in the folder
$files = m365 spo file list --webUrl $siteUrl --folderUrl $LibraryPath --output json | ConvertFrom-Json
}
# Download each file
foreach ($file in $files) {
$fileUrl = $file.ServerRelativeUrl
$fileName = $file.Name
$relpath = $fileUrl.SubString($folder.ServerRelativeUrl.length+1).Replace($fileName,"")
$localFilePath = $LocalDownloadPath
if($relpath.length -gt 0){
$localFilePath = Join-Path -Path $LocalDownloadPath -ChildPath $relpath
}
# Ensure the local download path exists
if (-not (Test-Path -Path $localFilePath)) {
$localFolder = New-Item -ItemType Directory -Path $localFilePath
}
Write-Host "Downloading $fileName..."
m365 spo file get --webUrl $siteUrl --url $fileUrl --asFile --path (Join-Path -Path $localFilePath -ChildPath $fileName)
}
}
# Call the function to download files
Download-FilesFromSharePoint -LibraryPath $libraryPath -LocalDownloadPath $localDownloadPath
Write-Host "Download completed."
```
[!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)]

***

## Contributors
Expand Down
11 changes: 10 additions & 1 deletion scripts/spo-download-files-from-doclib/assets/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
""
],
"creationDateTime": "2024-09-21",
"updateDateTime": "2024-09-21",
"updateDateTime": "2024-10-01",
"products": [
"SharePoint"
],
Expand All @@ -21,6 +21,10 @@
{
"key": "POWERSHELL",
"value": "7.2.0"
},
{
"key": "CLI-FOR-MICROSOFT365",
"value": "9.1.0"
}
],
"categories": [
Expand Down Expand Up @@ -50,6 +54,11 @@
"name": "Want to learn more about PnP PowerShell and the cmdlets",
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
"url": "https://aka.ms/pnp/powershell"
},
{
"name": "Want to learn more about CLI for Microsoft 365 and the commands",
"description": "Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.",
"url": "https://aka.ms/cli-m365"
}
]
}
Expand Down

0 comments on commit de6d7ba

Please sign in to comment.