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.

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

  #Must be SharePoint Administrator URL
  [Parameter(Mandatory = $true)]
  [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

#$SPOAdminUrl = “”

$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) 
    $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