Recently, I had a requirement to construct the OneDrive URL from the email address. As many of us know, OneDrive is a cloud storage service provided by Microsoft, which lets the user store up to 25 TB. Users are required to have a proper subscription (E1, E3, E5, etc) to utilize this service.
A couple of points to note, if you observe the URL of the One Drive for an organization, let’s consider Contoso. If the email of the user is vinay.deep@contoso.com, (usually the email address will be in the format of firstname.lastname@organizationname.com). Then the one drive URL would be:hostforlifebanner
https://contoso-my.sharepoint.com/personal/vinay_deep_contoso_com/_layouts/15/OneDrive.aspx  
Let’s try understanding by breaking down the URL,
  • https://contoso-my.sharepoint.com – This will be in format https://[YOURORGNAME]-my.sharepoint.com
  • /personal – it is the managed path defined for all OneDrive sites
  • /vinay_deep_contoso.com – observe the email address is vinay.deep@contoso.com. It replaces special characters with ‘_’.
  • /_layouts/15/onedrive.aspx – it is constant to access one drive pages

Steps

To get the final OneDrive URL from the given email address, I have followed the below steps. At a glance,
  • Getting the user input from the command prompt,
  • Extracting the org name from the email.
  • Building the URL string from email, basically replacing special characters with underscore ‘_’.
  • Constructing the URL.
I have used Powershell with a regular expression to achieve the result set.
Step 1
Getting the user input from the command prompt. Just read the user input from the command prompt and storing in $Email variable,

Step 2

Extracing the org name from the given email address. Here I am storing the regex pattern in variable called ‘$pattern’ and then finally matching with given email to extract the string which is organization name here,

Step 3

Building the One Drive string from the email. To replace the special character, I have written the function here called ‘Construct-OneDriveString’ from the given email.

Step 4
Finally, constructing the OneDrive URL from the values generated from above steps. We have got the OneDrive string and organization name, which is enough to build the URL.
Complete Script

In this script, after completing the construction of the OneDrive URL, I am also checking the connection to One Drive by using Invoke-WebRequest function, which is built in a function used to invoke a web request to any https/http URLs and have the response back.

If the resultant OneDrive URL is valid, the user will be given a success message called ‘connection verified’.
If the resultant OneDrive URL is not valid, the user will be given a failure message called ‘URL doesn’t exist.
Conclusion
Thus, in this article, we have seen how to construct the OneDrive URL from the user’s email input. Hope you find this article useful.