一、只導出文件列表的方法
1) 保存腳本(建議名:D:\tmp\Export-SharePoint-FileList.ps1
)
<#
導出 SharePoint 指定文件夾(含子文件夾)的文件列表到 CSV(不統計大小)
前提:已安裝 PowerShell 7 + PnP.PowerShell;已在 Entra 注冊并拿到 ClientId
#># =====[ 需要修改的 3 個參數 ]=====
$SiteUrl = "https://<你的租戶>.sharepoint.com/sites/<站點名>" # 例:https://suniatechnologyinc.sharepoint.com/sites/SuniaDUP2
$ClientId = "<你的ClientID>" # 你們自建的 App Registration 的 Application (client) ID
$FolderSiteRelativeUrl = "Shared Documents/路徑/到/目標目錄" # 例:Shared Documents/5 - Projects/2025 - OCR - EDU/1 - Raw Data/1 - Shenzen/OCR_data_collection_china
# 也可以用“文檔/...”;以瀏覽器地址欄顯示為準,注意把 %20 換成空格# =====[ 連接站點 ]=====
Connect-PnPOnline -Url $SiteUrl -ClientId $ClientId -Interactive# =====[ 導出 ]=====
$exportDir = "D:\tmp"
if (-not (Test-Path $exportDir)) { New-Item -ItemType Directory -Path $exportDir | Out-Null }
$outCsv = Join-Path $exportDir ("SharePoint_FileList_{0}.csv" -f (Get-Date -Format "yyyyMMdd_HHmmss"))# 遞歸獲取文件(不取大小,避免各種字段/閾值問題)
$files = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderSiteRelativeUrl -Recursive |Where-Object { $_.ItemType -eq "File" } |Select-Object Name, ServerRelativeUrl, TimeLastModified$files | Export-Csv $outCsv -NoTypeInformation -Encoding UTF8
Write-Host "文件清單已導出: $outCsv" -ForegroundColor Cyan
提示:
$FolderSiteRelativeUrl
必須是站點相對路徑(不帶/sites/...
前綴)。從瀏覽器地址欄復制/sites/<站點名>/...
后,去掉前面的/sites/<站點名>/
即可;把%20
改成空格。
2) 運行腳本
# 第一次運行腳本需要(當前用戶范圍即可)
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned# 執行
pwsh -File "D:\tmp\Export-SharePoint-FileList.ps1"
# 或者在 PowerShell 7 里:
# cd D:\tmp
# .\Export-SharePoint-FileList.ps1
3) 常見問題(只針對“導出列表”場景)
-
結果為空或報錯 “Not well formatted JSON stream”
先重新Connect-PnPOnline
,再運行;網絡不穩也會觸發,重試即可。 -
報 5000 閾值
僅導出名稱/路徑一般很少觸發。如果依然觸發,說明目錄極大:- 先在網頁里確認是否可以進一步細分子目錄;
- 分多次填不同的
$FolderSiteRelativeUrl
跑即可。
-
路徑不對
把瀏覽器里/sites/<站點名>/
后面的部分(Shared Documents/...
或文檔/...
)原樣粘貼到$FolderSiteRelativeUrl
。 -
庫名顯示為“文檔”但路徑用哪個?
以地址欄為準:地址如果是/Shared%20Documents/...
就寫Shared Documents/...
;如果是/文檔/...
就寫文檔/...
。
如果你之后還需要對比本地與云端差異,我可以在這個腳本基礎上增加一個“比對本地目錄文件名 & 路徑”的步驟;但你現在只要列表,這個版本最穩、最省事。
二、導出文件列表并統計大小的方法
Q1:為什么要用 PowerShell 而不是直接在網頁導出?
-
網頁端導出 Excel 功能對文檔庫視圖有 5000 項限制,大目錄經常超限
-
PowerShell 可以直接通過 SharePoint API 分頁拉取,不受單頁限制
-
可一次性導出:
- 文件名
- 文件路徑
- 文件大小(MB)
- 最后修改時間
- 目錄總大小
Q2:準備工作
-
安裝 PowerShell 7(64 位版)
下載地址
選擇PowerShell-<版本>-win-x64.msi
安裝 -
安裝 PnP.PowerShell 模塊:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
-
Azure Entra ID 全局管理員注冊一個 App(一次性操作):
-
名稱隨意(例如
PnP.PowerShell
) -
重定向 URI 選
http://localhost
-
分配 Microsoft Graph API 權限:
Sites.Read.All
Sites.ReadWrite.All
-
點擊“Grant admin consent”授權
-
復制 Application (client) ID(后續連接用)
-
Q3:執行腳本
-
連接到 SharePoint 站點:
Connect-PnPOnline -Url "https://<租戶名>.sharepoint.com/sites/<站點名>" `-ClientId "<你的ClientID>" `-Interactive
-
設置目標目錄路徑(Server Relative URL)
-
必須以
/sites/...
開頭 -
例如:
/sites/SuniaDUP2/Shared Documents/5 - Projects/2025 - OCR - EDU/1 - Raw Data/1 - Shenzen/OCR_data_collection_china
-
-
運行導出腳本(輸出到
D:\tmp
):$folderServerRelativeUrl = "/sites/SuniaDUP2/Shared Documents/5 - Projects/2025 - OCR - EDU/1 - Raw Data/1 - Shenzen/OCR_data_collection_china" $exportDir = "D:\tmp" if (-not (Test-Path $exportDir)) { New-Item -ItemType Directory -Path $exportDir | Out-Null } $outCsv = Join-Path $exportDir ("SharePoint_FileList_{0}.csv" -f (Get-Date -Format "yyyyMMdd_HHmmss"))$items = Get-PnPListItem -List "Documents" -PageSize 2000 `-FolderServerRelativeUrl $folderServerRelativeUrl `-Fields "FileLeafRef","FileRef","File_x0020_Size","Modified"$files = $items | Where-Object { $_.FileSystemObjectType -eq "File" } |Select-Object `@{Name="Name"; Expression={ $_.FieldValues["FileLeafRef"] }},@{Name="Path"; Expression={ $_.FieldValues["FileRef"] }},@{Name="SizeBytes"; Expression={ [int64]$_.FieldValues["File_x0020_Size"] }},@{Name="SizeMB"; Expression={ "{0:N2}" -f ( [double]$_.FieldValues["File_x0020_Size"] / 1MB ) }},@{Name="Modified"; Expression={ $_.FieldValues["Modified"] }}$files | Export-Csv $outCsv -NoTypeInformation -Encoding UTF8 Write-Host "文件清單已導出: $outCsv" -ForegroundColor Cyan$totalMB = ($files | Measure-Object -Property SizeBytes -Sum).Sum / 1MB Write-Host ("目錄總大小: {0:N2} MB" -f $totalMB) -ForegroundColor Green
Q4:常見問題
-
執行腳本時提示找不到列表
-
可能是文檔庫名稱不是
"Documents"
(中文環境可能顯示為“文檔”)
用以下命令查找:Get-PnPList | Where-Object { $_.BaseTemplate -eq 101 } | Select-Object Title, RootFolder
-
-
報 5000 項閾值錯誤
- 目錄中文件數過多時,拆分為多個子目錄分別導出
- 或使用 SharePoint “存儲指標”查看總大小(見上一份 FAQ)
-
導出文件顯示大小為 0
- 檢查是否正確獲取了
File_x0020_Size
字段 - 確保
Where-Object { $_.FileSystemObjectType -eq "File" }
過濾掉了文件夾
- 檢查是否正確獲取了
Q5:對比“存儲指標”方法
方法 | 適合場景 | 優點 | 缺點 |
---|---|---|---|
存儲指標(Web) | 只看目錄大小 | 不下文件、不受5000限制 | 無法導出文件清單 |
PowerShell 腳本 | 導出文件列表+大小 | 可做對比分析 | 大目錄可能要分批跑 |