SharePoint Online: Get OneDrive for Business URL of a User using PowerShell
Requirement: PowerShell to Get OneDrive URL in SharePoint Online.
How to Get the OneDrive URL of a User?
My Sites in SharePoint On-premises became OneDrive for Business in SharePoint Online. OneDrive for Business is a cloud-based file storage and collaboration platform that is integrated with Office 365. It provides a centralized location for storing and sharing files, as well as real-time collaboration and communication capabilities. Typically, OneDrive sites are located at the URL: https://tenant-my.sharepoint.com/personal/upn (E.g., https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com, or https://crescent-my.sharepoint.com/personal/salaudeen_crescent_onmicrosoft_com).
To get a SharePoint Online user’s OneDrive site, follow these steps:
- Login to SharePoint Online Admin Center at https://YourDomain-Admin.SharePoint.com
- Click on the “User Profiles” link from the left navigation >> Click on the “Manage User Profiles” link under the “People” group.
- Search and pick the user profile to which you want to gain access. From the search result, click on the menu item “Manage Personal Site” from the context menu.
This takes you to the OneDrive site! (If the OneDrive site is already created.) Let’s get OneDrive for business URLs using PowerShell.
SharePoint Online: PowerShell to Get OneDrive URL
Let’s query the user profile to retrieve the Personal site URL. Before running the script, remember to set the user name and admin site URL parameters.
#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
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$UserAccount="salaudeen@crescent.com"
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)
$Ctx.Load($User)
$Ctx.ExecuteQuery()
#Get OneDrive Site URL from User Profile
$PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
$UserProfile = $PeopleManager.GetPropertiesFor($User.LoginName)
$Ctx.Load($UserProfile)
$Ctx.ExecuteQuery()
#Get the User Profile Property
Write-host $UserProfile.PersonalUrl
}
Catch {
write-host -f Red "Error Getting User Profile Properties!" $_.Exception.Message
}
This script retrieves the OneDrive (MySite) URL of a given user. E.g., https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com/
PnP PowerShell to Get the OneDrive URL of a User:
Similarly, you can query the user profile and get My site URL with PnP PowerShell as,
#Set Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$UserAccount="salaudeen@crescent.com" #UPN
#Connect to the site
Connect-PnPOnline $AdminSiteURL -Interactive
#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
Open https://www.office.com/, and log in with your account >> Click on the App launcher >> click on “OneDrive”. This takes you to your OneDrive for Business main page. (URL shortcut: https://YourDomain-my.sharepoint.com).
More info: How to Open OneDrive for Business Site?
Login to Microsoft 365 Admin center at https://admin.microsoft.com >> Click on “Reports” in the left navigation >> Click on “Usage” >> Under OneDrive Files, Click on “View More”. This report provides you with a list of all OneDrive sites in your organization. You can export the report data into a CSV too.
More info: List all OneDrive for Business URLs
Wondering the same thing.
Love this script. I’m just wondering how I could use this script on a list of users? So import-csv with a list of UPNs then run the script from this?
Thanks!