Introduction
- 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 [email protected]. It replaces special characters with '_'.
- /_layouts/15/onedrive.aspx - it is constant to access one drive pages
Steps
- 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.
function Construct - OneDriveString {
param(
[string] $InputString,
[string] $Replacement = "_",
[string] $SpecialChars = ".@")
$rePattern = ($SpecialChars.ToCharArray() | ForEach - Object {
[regex]::Escape($_)
}) - join "|"
$InputString - replace $rePattern, $Replacement
}
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.
$OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx"
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.


function Construct-OneDriveString {
<#
.Description
This function takes the email input as string and replaces the special characters with '_' to construct OneDrive URL string
#>
param(
[string]$InputString,
[string]$Replacement = "_",
[string]$SpecialChars = ".@"
)
$rePattern = ($SpecialChars.ToCharArray() |ForEach-Object { [regex]::Escape($_) }) -join "|"
$InputString -replace $rePattern,$Replacement
}
$Email = Read-host "Enter the Email ID of user"
$OutputString = Construct-OneDriveString $Email
$pattern = '(?<=\@).+?(?=\.)'
$OrgName = [regex]::Matches($Email, $pattern).Value
$OneDriveUrl = "https://$OrgName-my.sharepoint.com/personal/" + $OutputString + "/_layouts/15/onedrive.aspx"
#Checking the URL
try {
Write-Host "verifying the url $OneDriveUrl" - ForegroundColor Yellow
$CheckConnection = Invoke-WebRequest -Uri $OneDriveUrl
if ($CheckConnection.StatusCode -eq 200) {
Write-Host "Connection Verified" -ForegroundColor Green
}
} catch [System.Net.WebException] {
$ExceptionMessage = $Error[0].Exception
if ($ExceptionMessage -match "403") {
Write-Host "URL exists, but you are not authorized" -ForegroundColor Yellow
}
elseif($ExceptionMessage -match "503") {
Write-Host "Server Busy" -ForegroundColor Red
}
elseif($ExceptionMessage -match "404") {
Write-Host "URL doesn't exists" -ForegroundColor Red
}
else {
Write-Host "There is an error" - ForegroundColor Red
}
}
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.
References
- https://powershellone.wordpress.com/2021/02/24/using-powershell-and-regex-to-extract-text-between-delimiters
- https://stackoverflow.com/questions/20259251/powershell-script-to-check-the-status-of-a-url

Vinay AyinapurapuPosted Oct 9, 2023, 2:33 PM
To my understanding it depends on the account set up. I would get in touch with the Team that is setting up the accounts. The One Drive URL is set based on how the account is set up.
Prasanna NandanPosted Oct 9, 2023, 6:00 AM
Hi Vinay, I my case where one of the user URL Is "/vinay_deep_contoso.com2" but i need to change it back to "/vinay_deep_contoso.com", is there a way to achieve this.