Get OneDrive for Business Site URL for Users using CSOM and PowerShell

Get OneDrive Site URL for Users using SharePoint Online Client Object Model and PowerShell.  This is a very basic script but useful.  I have seen some scripts and people are still usin the traditional SharePoint User Profile Web Service Report for reporting from User profile system.  The script below can do the same job nicely.  You just specify the name of the user and it find the oneDirve Site URL.

param
(
  #Must be SharePoint Administrator URL
  [Parameter(Mandatory = $false)]
  [ValidateNotNullOrEmpty()]
  [string] $SPOAdminUrl,

  #Must be SharePoint Administrator URL
  [Parameter(Mandatory = $true)]
  [ValidateNotNullOrEmpty()]
  [string] $InputFilePath
)

$InputFilePath = “C:\Temp\OneDriveUsers.txt”

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”

$Users = Get-Content -Path $InputFilePath

if ($Users.Count -eq 0)
{
  Write-Host $(“Unexpected user count: [{0}]” -f $Users.Count) -ForegroundColor Red
  return
}

#$SPOAdminUrl = “https://sharepointmvp-admin.sharepoint.com/”

$Creds = Get-Credential -Message “Please enter SharePo”
$SPOCreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Creds.UserName,$Creds.Password)
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
Connect-SPOService -Url $SiteURL -Credential $Creds
$Context.Credentials = $SPOCreds

$profileloader = [Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($Context)
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Context) -ErrorAction Inquire

Write-Host “Script Completed”

foreach($aUser in $Users)
{
    Write-Host “Trying to access User Profile for Personal Site…” -ForegroundColor Yellow
   
    $ClaimsUserFormat = “i:0#.f|membership|$aUser”
   
    $UserProfile = $PeopleManager.GetPropertiesFor($ClaimsUserFormat) 
    $Context.Load($UserProfile)
    $Context.ExecuteQuery()
 
    $Profile = $UserProfile | Select-Object PersonalUrl
    $SiteUrl = $Profile.PersonalUrl
   
    if($SiteUrl.Length -gt 0)
    {
      Write-Host “Personal Site Found for $aUser Starting to Loading files…” -ForegroundColor Yellow
      $SiteUrl = $Url.Remove($Url.LastIndexOf(“/”),1)
      $Site = Get-SPOSite -Identity $SiteURL
      Write-Host “Site URL is $($SiteUrl)” -ForegroundColor Yellow
      $continue = $false
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s