Find accounts that don't have a certain EmailAddress/proxyAddress

Hi, we have a hybrid Exchange setup and we’re trying to find all mailboxes that don’t have 2 smtp email addresses / proxy addresses. I can check it on either the Exchange server or a DC, whichever would be easier, but below is what I’ve been trying.

Get-Mailbox -OrganizationalUnit "TEST OU" | ?{$_.EmailAddresses -like "*domain.mail.onmicrosoft.com*"}

This works fine in listing the mailboxes that have it assigned, but I need to find the ones that don’t. It’s a Monday and I can’t think of how to do the reverse, would I have to use a “If…Then” statement or something?

Also, there should be two smtp addresses, domain.onmicrosoft.com and domain.mail.onmicrosoft.com, so I need to search and make sure both are there.

Use -notlike instead of -like?

Tried that, it still lists everyone in the OU. Tried it with an * and without, same thing.

Are the “wrong” people in possession of a blank address? And I’m thinking that the field is actually a collection capable of holding multiple values - which means -notlike wouldn’t work. You’d need a -notcontains or something, but those don’t do wildcards. You might end up just enumerating every user and running through a ForEach to examine each one. I’m not sure AD is going to be able to filter this server-side for you.

Yeah the EmailAddresses property from Get-Mailbox can contain multiple addresses (X500, smtp, etc.) and each user should have a few already because of an email address policy. I tried it with -notcontains as well, still no luck :frowning:

Is there one mailbox that you know you can test against? If like works notlike should also work. I don’t have any that are missing.

get-mailbox -Filter {emailaddresses -notlike “*microsoft.com”}

get-mailbox -Filter {emailaddresses -like “*microsoft.com”} | select name,alias,servername,@{n=‘emailaddys’;e={($.emailaddresses | ?{$ -match ‘microsoft’})-join '; '}}

Just the one line:

get-mailbox -Filter {emailaddresses -notlike "*microsoft.com"}

is working, thanks!!!

np.

Wonder why the -notlike filter works before the pipe, but not after? Weird.

This is a bit too complicated for a one liner. Emailaddresses is a collection of objects. It would probably be easier to write a function to test them.

function checksmtp {
  param($adr)
  
  $found1 = $false
  $found2 = $false
  foreach ($a in $adr) {
    if ($a.prefixstring -eq 'smtp') {
      if ($a.addressstring -like "*dom1.com") {
        $found1 = $true
      }
      if ($a.addressstring -like "*dom2.com") {
        $found2	= $true
      }
    }
  }
  ($found1 -and $found2)
}

Get-Mailbox -OrganizationalUnit "TEST OU" | ? {-not (checksmtp($_.emailaddresses))}

It is a multi-valued property.

get-mailbox | ?{($.emailaddresses -join ‘;’) -notmatch ‘microsoft’}
get-mailbox | ?{($
.emailaddresses -join ‘;’) -match ‘microsoft’}