List all the OneDrive for Business users in Office 365 – Powershell

We can get the list of users with OneDrive for Business feature provisioned by using SharePoint Online UserProfileService with Powershell. As you know SharePoint is base for OneDrive as like some other services (Office 365 Groups, Planner, Teams, and etc..), so it’s users basic profile information obviously will be stored in SharePoint. You can get all the SharePoint Online users using SharePoint UserProfileService and look for the Personal Space object, users who have OneDrive for Business site alone have value for this property and other users do not have this property.

Note: The below script use CSOM (Client Object Model) code to retrieve all OneDrive sites. If you need solution without CSOM, you can refer this post: Find and Export all OneDrive for Business users using Powershell

Steps to Export OneDrive for Business Provisioned Users

  • Fetch all SharePoint Online users using UserProfileService.
  • Find if OneDrive is provisioned or not.
  • Export my site collections to text file.

Note: Replace the variable <your tenant name> with your Office 365 tenant name in all the occurrences and provide your own admin credentials.

# Specify sharepoint online admin url 
$adminURL = "https://<your tenant name>-admin.sharepoint.com"

# Specify the location where the list of OneDrive sites should be saved
$LogFile = 'C:\OneDrivesites.txt'

#Add required references to SharePoint client assembly to use CSOM 
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")

#Provide your global admin credentials.
$UserName = "admin@<your tenant name>.onmicrosoft.com"
$SecPwd = $(ConvertTo-SecureString 'password' -asplaintext -force) 
$adminCreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,$SecPwd) 

# Set User Profile Service path using SPO admin URL 
$proxyaddr = "$adminURL/_vti_bin/UserProfileService.asmx?wsdl"
# Create a new webservice proxy to access UserProfileService
$UserProfileService= New-WebServiceProxy -Uri $proxyaddr -UseDefaultCredential False
$UserProfileService.Credentials = $adminCreds

# Set authentication cookies
$strAuthCookie = $adminCreds.GetAuthenticationCookie($adminURL)
$uri = New-Object System.Uri($adminURL)
$container = New-Object System.Net.CookieContainer
$container.SetCookies($uri, $strAuthCookie)
$UserProfileService.CookieContainer = $container

# Sets the first User profile, at index -1
$UserProfileResult = $UserProfileService.GetUserProfileByIndex(-1)

Write-Host "Starting- This could take a while."

$NumProfiles = $UserProfileService.GetUserProfileCount()
$i = 1

# As long as the next User profile is NOT the one we started with (at -1)...
While ($UserProfileResult.NextValue -ne -1) 
{
Write-Host "Checking profile $i of $NumProfiles" -foreground Yellow

# Look for the Personal Space object in the User Profile and retrieve it
# (PersonalSpace is the name of the path to a user's OneDrive for Business site. 
# Users who have not yet created a  OneDrive for Business site might not have this property)
$Prop = $UserProfileResult.UserProfile | Where-Object { $_.Name -eq "PersonalSpace" } 
$Url= $Prop.Values[0].Value

# If "PersonalSpace" (which we've copied to $Url) exists, log it to our file...
if ($Url) {
$siteUrl = "https://<your tenant name>-my.sharepoint.com"+ $Url
# Write OneDrive site url in console
Write-Host $Url -foreground Green
$siteUrl | Out-File $LogFile -Append -Force
}

# And now we check the next profile the same way...
$UserProfileResult = $UserProfileService.GetUserProfileByIndex($UserProfileResult.NextValue)
$i++
}

Source : https://technet.microsoft.com/en-us/library/dn911464.aspx


Advertisement

1 thought on “List all the OneDrive for Business users in Office 365 – Powershell”

Leave a Comment