Export OneDrive Sites
This script exports all onedrive sites to CSV
Summary
This sample shows how to export all onedrive sites to CSV. This report includes all columns. for eg. Url, Owner, Storage and etc.

$adminSiteURL = "https://domain-admin.sharepoint.com/"
$username = "username@domain.onmicrosoft.com"
$password = "********"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
$dateTime = "_{0:MM_dd_yy}_{0:HH_mm_ss}" -f (Get-Date)
$basePath = "E:\Contribution\PnP-Scripts\Logs\"
$csvPath = $basePath + "\OneDriveSites_PnP" + $dateTime + ".csv"
$global:onedriveSitesCollection = @()
Function Login() {
[cmdletbinding()]
param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $creds)
Write-Host "Connecting to Site '$($adminSiteURL)'" -f Yellow
Connect-PnPOnline -Url $adminSiteURL -Credential $creds
Write-Host "Connection Successful" -f Green
}
Function GetOnedriveSitesDeails {
try {
Write-Host "Getting onedrive sites..." -ForegroundColor Yellow
$global:onedriveSitesCollection = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'" | select *
}
catch {
Write-Host "Error in getting onedrive sites:" $_.Exception.Message -ForegroundColor Red
}
Write-Host "Exporting to CSV..." -ForegroundColor Yellow
$global:onedriveSitesCollection | Export-Csv $csvPath -NoTypeInformation -Append
Write-Host "Exported to CSV successfully!" -ForegroundColor Green
}
Function StartProcessing {
Login($creds);
GetOnedriveSitesDeails
}
StartProcessing$adminSiteURL = "https://domain-admin.sharepoint.com/"
$username = "username@domain.onmicrosoft.com"
$password = "********"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $secureStringPwd
$dateTime = "_{0:MM_dd_yy}_{0:HH_mm_ss}" -f (Get-Date)
$basePath = "E:\Contribution\PnP-Scripts\Logs\"
$csvPath = $basePath + "\OneDriveSites_SPO" + $dateTime + ".csv"
$global:onedriveSitesCollection = @()
Function Login() {
[cmdletbinding()]
param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $creds)
Write-Host "Connecting to Site '$($adminSiteURL)'" -f Yellow
Connect-SPOService -Url $adminSiteURL -Credential $creds
Write-Host "Connection Successful" -f Green
}
Function GetOnedriveSitesDeails {
try {
Write-Host "Getting onedrive sites..." -ForegroundColor Yellow
$global:onedriveSitesCollection = Get-SPOSite -Template "SPSPERS" -limit ALL -includepersonalsite $True | select *
}
catch {
Write-Host "Error in getting onedrive sites:" $_.Exception.Message -ForegroundColor Red
}
Write-Host "Exporting to CSV..." -ForegroundColor Yellow
$global:onedriveSitesCollection | Export-Csv $csvPath -NoTypeInformation -Append
Write-Host "Exported to CSV successfully!" -ForegroundColor Green
}
Function StartProcessing {
Login($creds);
GetOnedriveSitesDeails
}
StartProcessingContributors
| Author(s) |
|---|
| Chandani Prajapati |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Introduction to the SharePoint Online Management Shell
Check out the SPO Management Shell documentation site to get started and for the reference to the cmdlets.