在 Microsoft 365 中導出 OneDrive 使用情況報告

Jacki

用戶可能會佔用 OneDrive 存儲中的大量空間。通過 OneDrive 大小報告,您可以查看哪些用戶幾乎達到了限制。然後,您可以升級 OneDrive 存儲或告訴他們清理它。在本文中,您將了解如何在 Microsoft 365 中導出 OneDrive 使用情況報告。

若要獲取 Microsoft 365 中用戶的 OneDrive 存儲已用大小,請按照以下步驟操作:

  1. 登錄到Microsoft 365 管理中心
  2. 點擊用戶 > 活躍用戶
  3. 單擊用戶從列表中
  1. 點擊雲端硬盤
  2. 檢查使用的存儲空間部分

優點是 OneDrive 存儲使用的數據會在幾分鐘內填充。

在 Microsoft 365 管理中心導出 OneDrive 使用情況報告

要導出 Microsoft 365 中所有用戶的 OneDrive 使用情況報告,請按照以下步驟操作:

  1. 登錄到Microsoft 365 管理中心
  2. 點擊設置 > 組織設置 > 服務
  1. 點擊報告
  2. 取消選中在所有報告中顯示隱藏的用戶、組和站點名稱
  3. 點擊節省
  1. 點擊報告 > 使用情況
  2. 點擊雲端硬盤並向下滾動到底部
  3. 點擊出口下載 OneDrive 使用情況報告 CSV 文件

該報告的缺點是數據不是立即更新的。報告將在 48 小時內提供。

使用 PowerShell 腳本導出 OneDrive 使用情況報告

Get-OneDriveSizeReport.ps1 PowerShell 腳本將獲取所有用戶的 OneDrive 大小並輸出以下信息:

  1. 所有者
  2. 統一網絡
  3. 站點ID
  4. 已刪除
  5. 上次活動
  6. 文件計數
  7. 活動文件計數
  8. 配額GB
  9. 二手GB
  10. 使用百分比
  11. 城市
  12. 國家
  13. 部門
  14. 職稱

筆記:該腳本檢查組織中是否啟用了報告中隱藏的用戶數據。如果是這樣,它會暫時禁用它,報告完成後,它會再次啟用它。

要使用 PowerShell 導出所有用戶的 OneDrive 使用情況報告,請按照以下步驟操作:

步驟 1. 安裝 Microsoft Graph PowerShell

以管理員身份運行 Windows PowerShell 並安裝 Microsoft Graph PowerShell。

Install-Module Microsoft.Graph -Force

重要的:在運行 cmdlet 或腳本之前,請務必更新到最新的 Microsoft Graph PowerShell 模塊版本,以防止出現錯誤和不正確的結果。

步驟 2. 下載 Get-OneDriveSizeReport PowerShell 腳本

上創建兩個文件夾(中:)駕駛:

  • 腳本
  • 溫度

下載 Get-OneDriveSizeReport.ps1 PowerShell 腳本並將其放置在C:腳本文件夾。該腳本會將所有用戶 OneDrive 大小導出到以下位置的報告中:C:溫度文件夾。

確保文件未被阻止,以防止運行腳本時出現錯誤。請閱讀文章運行 PowerShell 腳本時出現未數字簽名錯誤來了解更多信息。

另一種選擇是將以下代碼複製並粘貼到記事本中。給它起個名字獲取 OneDriveSizeReport.ps1並將其放置在C:腳本文件夾。

<#
    .SYNOPSIS
    Get-OneDriveSizeReport.ps1

    .DESCRIPTION
    Export OneDrive storage usage in Microsoft 365 to CSV file with PowerShell.

    .LINK
    www.alitajran.com/export-onedrive-usage-report/

    .NOTES
    Written by: ALI TAJRAN
    Website:    www.alitajran.com
    LinkedIn:   linkedin.com/in/alitajran

    .CHANGELOG
    V1.00, 08/10/2024 - Initial version
#>

# Connect to Microsoft Graph with necessary permissions
Connect-MgGraph -NoWelcome -Scopes "User.Read.All", "Reports.Read.All", "ReportSettings.ReadWrite.All"

# Define file paths
$CSVOutputFile = "C:tempOneDriveSizeReport.csv"
$TempExportFile = "C:tempTempExportFile.csv"

# Remove the temporary export file if it exists
if (Test-Path $TempExportFile) {
    Remove-Item $TempExportFile
}

# Check if tenant reports have concealed user data, and adjust settings if necessary
if ((Get-MgAdminReportSetting).DisplayConcealedNames -eq $true) {
    $Parameters = @{ displayConcealedNames = $false }
    Write-Host "Unhiding concealed report data to retrieve full user information..." -ForegroundColor Cyan
    Update-MgAdminReportSetting -BodyParameter $Parameters
    $ConcealedFlag = $true
}
else {
    $ConcealedFlag = $false
    Write-Host "User data is already fully visible in the reports." -ForegroundColor Cyan
}

# Retrieve detailed user account information
Write-Host "Fetching user account details from Microsoft Graph..." -ForegroundColor Cyan

# Define user properties to be retrieved
$Properties = 'Id', 'displayName', 'userPrincipalName', 'city', 'country', 'department', 'jobTitle', 'officeLocation'

# Define parameters for retrieving users with assigned licenses
$userParams = @{
    All              = $true
    Filter           = "assignedLicenses/`$count ne 0 and userType eq 'Member'"
    ConsistencyLevel = 'Eventual'
    CountVariable    = 'UserCount'
    Sort             = 'displayName'
}

# Get user account information and select the desired properties
$Users = Get-MgUser @UserParams -Property $Properties | Select-Object -Property $Properties

# Create a hashtable to map UPNs (User Principal Names) to user details
$UserHash = @{}
foreach ($User in $Users) {
    $UserHash[$User.userPrincipalName] = $User
}

# Retrieve OneDrive for Business site usage details for the last 30 days and export to a temporary CSV file
Write-Host "Retrieving OneDrive for Business site usage details..." -ForegroundColor Cyan
Get-MgReportOneDriveUsageAccountDetail -Period D30 -Outfile $TempExportFile

# Import the data from the temporary CSV file
$ODFBSites = Import-CSV $TempExportFile | Sort-Object 'User display name'

if (-not $ODFBSites) {
    Write-Host "No OneDrive sites found." -ForegroundColor Yellow
    return
}

# Calculate total storage used by all OneDrive for Business accounts
$TotalODFBGBUsed = [Math]::Round(($ODFBSites.'Storage Used (Byte)' | Measure-Object -Sum).Sum / 1GB, 2)

# Initialize a list to store report data
$Report = [System.Collections.Generic.List[Object]]::new()

# Populate the report with detailed information for each OneDrive site
foreach ($Site in $ODFBSites) {
    $UserData = $UserHash[$Site.'Owner Principal name']
    $ReportLine = [PSCustomObject]@{
        Owner             = $Site.'Owner display name'
        UserPrincipalName = $Site.'Owner Principal name'
        SiteId            = $Site.'Site Id'
        IsDeleted         = $Site.'Is Deleted'
        LastActivityDate  = $Site.'Last Activity Date'
        FileCount         = [int]$Site.'File Count'
        ActiveFileCount   = [int]$Site.'Active File Count'
        QuotaGB           = [Math]::Round($Site.'Storage Allocated (Byte)' / 1GB, 2)
        UsedGB            = [Math]::Round($Site.'Storage Used (Byte)' / 1GB, 2)
        PercentUsed       = [Math]::Round($Site.'Storage Used (Byte)' / $Site.'Storage Allocated (Byte)' * 100, 2)
        City              = $UserData.city
        Country           = $UserData.country
        Department        = $UserData.department
        JobTitle          = $UserData.jobTitle
    }
    $Report.Add($ReportLine)
}

# Export the report to a CSV file and display the data in a grid view
$Report | Sort-Object UsedGB -Descending | Export-CSV -NoTypeInformation -Encoding utf8 $CSVOutputFile
$Report | Sort-Object UsedGB -Descending | Out-GridView -Title OneDriveUsageReport

Write-Host ("Current OneDrive for Business storage consumption is {0} GB. Report saved to {1}" -f $TotalODFBGBUsed, $CSVOutputFile) -ForegroundColor Cyan

# Reset tenant report data concealment setting if it was modified earlier
if ($ConcealedFlag -eq $true) {
    Write-Host "Re-enabling data concealment in tenant reports..." -ForegroundColor Cyan
    $Parameters = @{ displayConcealedNames = $true }
    Update-MgAdminReportSetting -BodyParameter $Parameters
}

# Clean up the temporary export file
if (Test-Path $TempExportFile) {
    Remove-Item $TempExportFile
    Write-Host "Temporary export file removed." -ForegroundColor Cyan
}
  • 第 24/25 行:編輯 CSV 文件路徑

步驟 3. 運行 Get-OneDriveSizeReport PowerShell 腳本

運行以下命令來運行獲取 OneDriveSizeReport.ps1PowerShell 腳本。

c:scripts.Get-OneDriveSizeReport.ps1

將出現以下輸出。

Unhiding concealed report data to retrieve full user information...
Fetching user account details from Microsoft Graph...
Retrieving OneDrive for Business site usage details...
Current OneDrive for Business storage consumption is 124,19 GB. Report saved to C:tempOneDriveSizeReport.csv
Re-enabling data concealment in tenant reports...
Temporary export file removed.

報告輸出將發送到單獨窗口 (Out-GridView) 中的交互式表格。

步驟 4. 檢查 OneDrive 使用情況報告 CSV 文件

Get-OneDriveSizeReport.ps1 PowerShell 腳本將所有 Microsoft 365 用戶 OneDrive 大小導出到 CSV 文件。

找到文件OneDriveSizeReport.csv在路徑中C:溫度

使用您喜歡的應用程序打開 CSV 文件。在我們的示例中,它是 Microsoft Excel。

另請閱讀:導出 Microsoft 365 用戶上次密碼更改日期和時間

就是這樣!

筆記:從 2025 年 1 月開始,微軟將向組織收取未經許可的 OneDrive 帳戶存儲費用。了解哪些帳戶未經許可的一個好方法是獲取未經許可的 OneDrive 用戶帳戶報告

結論

您了解瞭如何在 Microsoft 365 中導出 OneDrive 使用情況報告。如果您想立即檢查用戶的 OneDrive 大小數據,請在 Microsoft 365 管理中心查找該用戶。若要獲取包含更多詳細信息的報告,請使用 Microsoft 365 管理中心中的報告部分或使用 PowerShell。缺點是需要等待48小時才能獲得數據。

您喜歡這篇文章嗎?您可能還喜歡使用 PowerShell 腳本的 Microsoft 365 安全建議。不要忘記關注我們並分享這篇文章。