SharePoint Online: Get OneDrive for Business URL of a User using PowerShell

Requirement: PowerShell to Get OneDrive URL in SharePoint Online.

How to Get OneDrive URL of a User?
My Sites in SharePoint On-premises bacame OneDrive for Business in SharePoint Online. Typically, OneDrive sites are located at the URL:  (E.g., or, To get a SharePoint Online user's OneDrive site, follow these steps:
  • Login to SharePoint Online Admin Center
  • Click on "User Profiles" link from left navigation >> Click on "Manage User Profiles" link under "People" group
    get onedrive url for user powershell
  • Search and pick the user profile which you want to gain access. From the search result, Click on the menu item "Manage Personal Site" from the context menu.
    SharePoint Online PowerShell to Get OneDrive URL
This takes you to the OneDrive site! Let's get OneDrive for business URL using PowerShell.
You can also get OneDrive site of a user from Office 365 admin center! Login to Office 365 Admin Centre at, Search and find the user. From user settings page, Click on the OneDrive tab >> Click on "Create link to files" and you'll see the link to user's OneDrive site!

SharePoint Online: PowerShell to Get OneDrive URL
Let's query user profile to retrieve Personal site URL. Remember to set the user name and admin site URL parameters prior running the script.
#Load SharePoint CSOM Assemblies
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"

#Set Config Parameters
$UserAccount="[email protected]"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    #Get the User
    $User = $Ctx.web.EnsureUser($UserAccount)
    #Get OneDrive Site URL from User Profile
    $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
    $UserProfile = $PeopleManager.GetPropertiesFor($User.LoginName)

    #Get the User Profile Property
    Write-host $UserProfile.PersonalUrl
Catch {
    write-host -f Red "Error Getting User Profile Properties!" $_.Exception.Message
This script retrieves OneDrive (MySite) URL of a given user. E.g.

PnP PowerShell to Get OneDrive URL of a User:
Similarly, you can query user profile and get My site URL with PnP PowerShell as,
#Set Config Parameters
$UserAccount="[email protected]" #UPN

#Connect to the site
Connect-PnPOnline $AdminSiteURL -Credentials (Get-Credential)

#powershell to get onedrive for business url
Get-PnPUserProfileProperty -Account $UserAccount | Select PersonalUrl
Here is another post to get all OneDrive sites collections of the Tenant: Find All OneDrive Site Collections in SharePoint Online using PowerShell

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.