Reverse engineering email address from OneDrive personal site Url

Sup’ PSHomies,

Migrations… Gotta love ’em!!!

I’m doing a OneDrive for Business tenant to tenant migration using Sharegate. If you ever decide to do any sort of SharePoint affiliated migration, best to use Sharegate! Their support is top notch! Shout out to Jimmy De Santis!

Sharegate has a cmdlet Get-OneDriveUrl that will retrieve or provision you ODFB URL (OneDrive For Business). Depending on a user’s Primary SMTP Address the URL format may not be what you expected.

Here’s a link to the script to get your ODFB information of your organization.  Be sure to follow the setup instructions.

This will generate a simple text file (I used it as-is, a rare occasion, I know… Don’t judge me…). To generate the URL, you need to swap any ‘.’ and ‘@’ for ‘_’.  I’m looking to reverse this process. Why? Well because I hate manual work. I’m almost certain I have to do this again sometime in the near future…

Those who forget to automate are doomed to repeat their work…

Words to live by…

My hunch paid off! By reverse engineering the email address from the ODFB URL, I could use that email address to retrieve the ODFB URL using Get-OneDriveUrl. I needed to provision the URLs in the target domain, fortunately the email addresses in the Target tenant are uniform.

Ok here’s the code:

$personalSites = @"
"@ | ConvertFrom-Csv
$personalSites |
ForEach-Object {
#Get ODFB part
Try {
$arrODFBUrl = ($($_.personalSite).Split('//'))[-2].Split('_')
$count = @($arrODFBUrl).Count
$index = 1
$emailUsedForODFB = $arrODFBUrl[0]
Do {
if ($arrODFBUrl[$index] -like 'despacito*') {
$emailUsedForODFB = "$($emailUsedForODFB)@$($arrODFBUrl[$index])"
elseif ($arrODFBUrl[$index] -like 'simba*') {
$emailUsedForODFB = "$($emailUsedForODFB)@$($arrODFBUrl[$index])"
elseif ($arrODFBUrl[$index] -like 'sudoku*') {
$emailUsedForODFB = "$($emailUsedForODFB)@$($arrODFBUrl[$index])"
else {
$emailUsedForODFB = "$($emailUsedForODFB).$($arrODFBUrl[$index])"
}until($index -eq $count)
#Some ODFB may have a '1' at the end. Just remove that
if ($emailUsedForODFB -like '*nl1') {
$emailUsedForODFB = $emailUsedForODFB -replace ".$"
$samAccountName = $emailUsedForODFB.Split('@')[0].ToLower()
$targetEmail = '{0}' -f $samAccountName
SamAccountName = $samAccountName
SourceEmail = $emailUsedForODFB
SourcePersonalUrl = $($_.personalSite)
SourceGeneratedUrl = '{0}/' -f $emailUsedForODFB.Replace('@', '_').Replace('.','_').ToLower()
TargetEmail = $targetEmail
TargetGeneratedUrl = '{0}/' -f $targetEmail.Replace('@', '_').Replace('.','_').ToLower()
catch {
Write-Warning 'Not a valid personal site format'
} |

Having the email address I can generate the source URL and verify if it is what I expected it to be. The samaccountname remained the same, so it was easy enough to generate the new email address and the expected target URL which once I provision, can also be verified that it is what I expected it to be. Still with me? Just have a look at the image… 😉

Generated ODFB Urls

You can generate quite the information by just have a personal ODFB Url!

Not being content with just the generated information, I did one last check using Sharegate’s Get-OneDriveUrl:

$csvUrls = Import-Csv C:\scripts\sources\csv\VerifyUrls.csv -Delimiter "`t" -Encoding UTF8
#region Initialize Stuff
Import-Module ShareGate -Verbose:$false
# User account for an Office 365 global admin in your organization
$HashCreds = Import-CliXml -Path "${env:\userprofile}\Hash.Cred"
#Admin urls
$targetAdminUrl = ''
$sourceAdminUrl = ''
#Connect to Tenant Site
$targetTenantSite = Connect-Site -Url $targetAdminUrl -Credential $HashCreds.'urv-target-o365admin'
$sourceTenantSite = Connect-Site -Url $sourceAdminUrl -Credential $HashCreds.'irwins-source-o365admin'
Function Get-ODFBUrls{
#Provision using Sharegate and wait for it to finish.
$targetOneDriveUrl = Get-OneDriveUrl -Tenant $targetTenantSite -Email $targetUserEmail
$sourceOneDriveUrl = Get-OneDriveUrl -Tenant $sourceTenantSite -Email $sourceUserEmail
SourceEmail = $sourceUserEmail
SourceODFBUrl = $sourceOneDriveUrl
TargetEmail = $targetUserEmail
TargetODFBUrl = $targetOneDriveUrl
#region Get ODFB Urls
$csvUrls |
Get-ODFBUrls -sourceUserEmail $_.SourceEmail -targetUserEmail $_.TargetEmail
Export-Csv C:\scripts\export\ODFBUrls-27112017.csv -Encoding UTF8 -Delimiter "`t" -NoTypeInformation

This aligns the source and target URLs nicely! Now I can migrate from tenant to tenant without to much hassle… Well at least that’s the plan… 😉

Hope it’s worth something to you,




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s