$Creds = Get-Credential
$site = ‘https://tenant.sharepoint.com’
$spoadminsite = ‘https://tenant-admin.sharepoint.com’
Connect-MsolService -Credential $Creds
Connect-SPOService -Url $spoadminsite -Credential $Creds
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll”
#Get the Client Context and Bind the Site Collection
$context = New-Object Microsoft.SharePoint.Client.ClientContext($site)
#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName , $Creds.Password)
$context.Credentials = $credentials
#Fetch the users in Site Collection
$users = Get-MsolUser -All
#Create an Object [People Manager] to retrieve profile information
$people = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($context)
$context.ExecuteQuery()
$collection = @()
Foreach($user in $users)
{
$ClaimsUserFormat = “i:0#.f|membership|$($user.UserPrincipalName)”
$userprofile = $people.GetPropertiesFor($ClaimsUserFormat)
$context.Load($userprofile)
$context.ExecuteQuery()
Write-Host $userprofile.Email
Write-Host $user.LoginName
Write-Host $userprofile.AccountName
if($userprofile.AccountName -ne $null)
{
Write-Host “Email : ” $userprofile.Email
$upp = $userprofile.UserProfileProperties
$profileData = “” | Select “FirstName” , “LastName” , “AccountName” , “PersonalUrl” , “Email” , “LoginName”
$profileData.LoginName = $user.UserPrincipalName
$profileData.FirstName = $upp.FirstName
$profileData.LastName = $upp.LastName
$profileData.Email = $upp.WorkEmail
$profileData.AccountName = $upp.AccountName
$profileData.PersonalUrl = $userprofile.PersonalUrl
$collection += $profileData
}
}
$OneDrivePath= “C:\temp\SPO-UserInformation.csv”
$collection | Export-Csv $OneDrivePath -NoTypeInformation -Encoding UTF8
$OneDriveWithcollection = @()
$SitesData = IMport-csv -Path $OneDrivePath
foreach($Site in $SitesData)
{
$Url = $Site.PersonalUrl
if($Url.Contains(“Person.aspx”))
{
Write-host “No profile”
}
else
{
Write-host $Url
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
Write-Host $Site.LoginName
$SiteUrl = $Url.Remove($Url.Length-1, 1)
Set-SPOUser -Site $SiteUrl -LoginName $Creds.UserName -IsSiteCollectionAdmin $true
#Authenticate
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName , $Creds.Password)
$context.Credentials = $credentials
$Web = $context.Web
$context.Load($Web)
$List = $context.Web.Lists.GetByTitle(“Documents”)
$context.Load($List)
$context.ExecuteQuery();
Write-Host “Items Found :” $List.ItemCount
$OneDriveData = “” | Select “Email” , “ItemCount”, “LastItemModifiedDate”, “LastItemUserModifiedDate”
$OneDriveData.Email = $Site.Email
$OneDriveData.ItemCount = $List.ItemCount
$OneDriveData.LastItemModifiedDate = $Web.LastItemModifiedDate
$OneDriveData.LastItemUserModifiedDate = $Web.LastItemUserModifiedDate
$OneDriveWithcollection += $OneDriveData
}
}
$OneDriveWithItemsPath= “C:\temp\SPO-OneDriveItems.csv”
$OneDriveWithcollection | Export-Csv $OneDriveWithItemsPath -NoTypeInformation -Encoding UTF8
Update: The script is also available below
https://gallery.technet.microsoft.com/Get-OneDrive-for-Business-04b3632c