Adding a Second Outlook Account from a Different Domain Using PowerShell

NewInMSWorld 0 Reputation points
2025-03-27T07:46:42.45+00:00

An attempt was made to add or map a second Outlook account under the same profile through a PowerShell script, but it was unsuccessful despite the script reporting a successful status. Below is the PowerShell script used:

# Ensure Outlook is closed before making changes
Stop-Process -Name outlook -Force -ErrorAction SilentlyContinue
Start-Sleep -Seconds 3

# Ask for second email details
$SecondaryEmail = Read-Host "Enter the second email (e.g., ******@123xyz.onmicrosoft.com)"
$Password = Read-Host "Enter the password for $SecondaryEmail" -AsSecureString

# Convert password to plain text for use in the script (only during execution)
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

# Define Outlook Profile Registry Path
$ProfilePath = "HKCU:\Software\Microsoft\Office\16.0\Outlook\Profiles\Outlook"

# Ensure the Outlook Profile exists
If (!(Test-Path $ProfilePath)) {
    Write-Host "Creating new Outlook profile..."
    New-Item -Path $ProfilePath -Force | Out-Null
}

# Generate unique registry key for the new account
$AccountKey = (New-Guid).Guid
$NewAccountPath = "$ProfilePath\9375CFF0413111d3B88A00104B2A6676\$AccountKey"

# Add the secondary email account with IMAP and SMTP settings
Write-Host "Adding $SecondaryEmail to Outlook Profile..."
New-Item -Path $NewAccountPath -Force | Out-Null
Set-ItemProperty -Path $NewAccountPath -Name "AccountName" -Value $SecondaryEmail -Force
Set-ItemProperty -Path $NewAccountPath -Name "Email" -Value $SecondaryEmail -Force

# IMAP Settings
Set-ItemProperty -Path $NewAccountPath -Name "IMAPServer" -Value "outlook.office365.com" -Force
Set-ItemProperty -Path $NewAccountPath -Name "IMAPPort" -Value 993 -Force
Set-ItemProperty -Path $NewAccountPath -Name "IMAPUseSSL" -Value 1 -Force

# SMTP Settings
Set-ItemProperty -Path $NewAccountPath -Name "SMTPServer" -Value "smtp.office365.com" -Force
Set-ItemProperty -Path $NewAccountPath -Name "SMTPPort" -Value 587 -Force
Set-ItemProperty -Path $NewAccountPath -Name "SMTPUseTLS" -Value 1 -Force

# Store Password (Not recommended for production; consider secure vaults)
Set-ItemProperty -Path $NewAccountPath -Name "Password" -Value $PlainPassword -Force

# Restart Outlook
Start-Process "outlook.exe"
Write-Host "Successfully added $SecondaryEmail to Outlook with IMAP/SMTP. Restart Outlook to verify."

Despite the "Successfully added" message, the new account does not appear in the account settings. What steps could be taken to troubleshoot this issue?

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,345 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.