如何使用 PnP PowerShell 从 SharePoint 文档库下载文件?
最近,我需要从 SharePoint 的文档库下载文件。为此,我使用 PnP PowerShell。在本教程中,我将向您展示如何使用 PnP PowerShell 从 SharePoint 文档库下载文件。
我将向您展示如何使用 PnP PowerShell 从 SharePoint Online 文档库下载单个文件。此外,我还将向您展示如何使用 PnP PowerShell 从 SharePoint 文档库下载多个文件。
使用 PnP PowerShell 从 SharePoint 文档库下载文件
要使用 PnP PowerShell 从 SharePoint 文档库下载单个文件,您需要指定库中文件的路径以及要保存文件的本地路径。这是完整的代码:
$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
$libraryName = "HRDocuments"
$fileName = "MyWordFile.docx"
$localPath = "C:\Downloads\"
# Get the file
$file = Get-PnPFile -Url "/$libraryName/$fileName" -Path $localPath -FileName $fileName -AsFile
Write-Host "File downloaded to $localPath"执行上述 PowerShell 脚本后,它会从 SharePoint Online 文档库下载文档并将其保存在我的本地文件夹中。
在 PowerShell 中读取从 URL 下载文件
使用 PnP PowerShell 从 SharePoint 文档库下载多个文件
现在,让我向您展示如何使用 PnP PowerShell 从 SharePoint 文档库下载多个文件。
下面是用于从 SharePoint Online 文档库下载所有文件的 PnP PowerShell 脚本。
$siteUrl = "https://szg52.sharepoint.com/sites/PowerShellFAQs"
Connect-PnPOnline -Url $siteUrl -Interactive
# Define the library and local download path
$libraryName = "HRDocuments"
$localFolderPath = "C:\Downloads\"
# Ensure the local folder exists
if (-Not (Test-Path -Path $localFolderPath)) {
New-Item -ItemType Directory -Path $localFolderPath
}
# Get all files from the library
$files = Get-PnPListItem -List $libraryName | Where-Object { $_.FileSystemObjectType -eq "File" }
foreach ($file in $files) {
$fileUrl = $file.FieldValues.FileRef
$fileName = $file.FieldValues.FileLeafRef
$localFilePath = Join-Path -Path $localFolderPath -ChildPath $fileName
# Download the file
Get-PnPFile -Url $fileUrl -Path $localFolderPath -FileName $fileName -AsFile
Write-Host "File $fileName downloaded to $localFolderPath"
}执行上述 PowerShell 脚本后,它会将所有文件下载到本地文件夹。执行上述 PowerShell 脚本后,您可以在下面的屏幕截图中看到输出。
如果您想使用 PnP PowerShell 从 SharePoint Online 文档库批量下载文件,那么我希望上面的完整脚本会有所帮助。
结论
在本教程中,我解释了如何使用 PnP PowerShell 从 SharePoint Online 文档库下载单个文件以及如何在 SharePoint Online 中使用 PnP PowerShell 从文档库下载多个文件强>。
