Search code examples
powershelloffice365exchange-serverpowershell-3.0microsoft365

How to correctly know a mailbox exists in exchange online or not using PowerShell


I have created a powershell script to connects to exchange online. The way it works is that, the script accepts a commandline argument as input(userprincipalname of a user), retrieves all mailboxes in exchange online then it checks if the user issued userprincipalname matches mailbox in exchange online. If the mailbox does not exist, i'm writing host, "mailbox does not exist", if the mailbox exists, i'm writing host "mailbox exists."

Problem The problem is the scripts returns both if and else statement bodies. I expect to see if statement body returned only if the mailbox exists and else statement body returned only if the mailbox does not exist.

What I'm i doing wrong.

Below is the script.


param($m)
# Add your Global admin plain password here
$password_ = "mysecurepassword"
$password = ConvertTo-SecureString $password_ -AsPlainText -Force

# Add your global administrator login email here.
$upn = "[email protected]"

# Automated login to azure ad
$AppCredential = New-Object System.Management.Automation.PSCredential($upn, $password)
Connect-ExchangeOnline -Credential $AppCredential

# Retrieving all mailboxes in exchange online
$usermbxs = (Get-EXOMailbox).UserPrincipalName
foreach($usermbx in $usermbxs){

# Check if the user given mailbox exists in exchangeonline
if($m -match $usermbx){

write-host $m "Mailbox does exists"

}else{

write-host "The mailbox does not exist"

}

}

The output i get when i pass upn of a user who has mailbox in exchange online enter image description here

The output i get when i pass upn of a user who does not exist in exchange online enter image description here


Solution

  • What am I doing wrong?

    Ignoring the "put your global admin password in this script" requirement (you should be using modern authentication instead for better security - upgrade your PowerShell module for ExchangeOnline), your script is comparing each mailbox against the $m passed one at a time. Presuming your tenant has 6 accounts, it is checking each UserPrincipalName at a time, and advising if that UPN matches the $m param.

    $usermbxs = (Get-EXOMailbox).UserPrincipalName
    foreach($usermbx in $usermbxs){
    
    # Check if the user given mailbox exists in exchangeonline
    if($m -match $usermbx){
    
    write-host $m "Mailbox does exists"
    
    }else{
    
    write-host "The mailbox does not exist"
    
    }
    
    }
    

    You can simplify your code by removing the "foreach" check, and comparing using the -contains operator, which checks for the existence of a value in an collection, which would be the $usermbxs array. -contains is case insensitive - if (for whatever reason) you need case sensitive checking, use -ccontains.

    $usermbxs = (Get-EXOMailbox).UserPrincipalName
    if ($usermbxs -contains $m) {
    Write-Host "$m mailbox exists"
    } 
    else {
    Write-Host "$m mailbox does not exist"
    }