Since you are new to PS let me save some trouble and hopefully allow you to focus on writing your script vs figuring out something that isn’t very clear in the documentation.
First, let’s look at filtering with get-aduser.
I can search for users with a specific email domain. Works great.
Note the wildcard is on the left of the search string.
PS C:\PSS> get-aduser -Filter {mail -like "*mydomain.us"} -Properties mail | select name,mail
name mail
---- ----
Yogi Bear yogi.bear@mydomain.us
Ok, so we seem to know how to use the -filter option right? Well not so fast, if you try and apply what you just learned to get-mailbox, it fails.
PS C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like "*mydomain.us"}
PS C:\>
On the other hand, if I say give me all the email address that start with Yogi, I do get a return.
PS C:\> Get-Mailbox -Filter {PrimarySmtpAddress -like "yogi*"}
Name Alias ServerName ProhibitSendQuota
---- ----- ---------- -----------------
Yogi Bear yogi.bear exchange01 Unlimited
If I say give me all the mailboxes with the last name of bear, I also get a return.
PS C:\> Get-Mailbox -Filter {Name -like "*bear"}
Name Alias ServerName ProhibitSendQuota
---- ----- ---------- -----------------
Yogi Bear yogi.bear exchange01 Unlimited
Same problem for groups
PS C:\> Get-DistributionGroup -Filter {name -like "*group"}
Name DisplayName GroupType PrimarySmtpAddress
---- ----------- --------- ------------------
Test Group Test Group Universal test.goup@mydomain.us
PS C:\> Get-DistributionGroup -Filter {primarysmtpaddress -like "*mydomain.us"}
PS C:\> Get-DistributionGroup -Filter {primarysmtpaddress -like "test*"}
Name DisplayName GroupType PrimarySmtpAddress
---- ----------- --------- ------------------
Test Group Test Group Universal test.goup@mydomain.us
Same issue happens with get-unifiedgroup
PS C:\PSS> Get-UnifiedGroup -Filter {PrimarySMTPAddress -like "*mydomain.us"}
PS C:\PSS> Get-UnifiedGroup -Filter {PrimarySMTPAddress -like "c*"}
Name DisplayName GroupType PrimarySmtpAddress
---- ----------- --------- ------------------
CoolCatDirectReports-Privat_a7a1d50d-2796-4887-a677-6733d75ac404 Cool Cat Direct Reports - Private Universal CoolCatDirectRepo...
We unfortunately cannot apply the wild card on the left of the search with primarysmtpaddress attribute. Only wildcards to the right of the search string. which means
“mydomain” and “*mydomain” will not work for filtering on primarysmtpaddress.
If you need the wildcard to be on the left, you will need to use this option. It does work.
Get-UnifiedGroup | Where-Object {$_.PrimarySMTPAddress -like '*mydomain.us'}