Apr 01 2022 01:31 PM - edited Apr 01 2022 01:31 PM
I'm looking to find office365 users whose primary SMTP is our domain.com that are not part of certain office365 distribution lists we have. For example Dist1, Dist2, Dist3, Dist4 and export the results to a csv. I'm hoping someone already created such a script. Any is appreciated. Thank you
Apr 02 2022 03:16 AM - edited Apr 02 2022 03:21 AM
@Sam240 I created this one in my test tenant:
Script:
Connect-ExchangeOnline
$total = @()
$users = Get-Mailbox | Where-Object PrimarySmtpAddress -Match 'M365x537152.OnMicrosoft.com' | where-object PrimarySmtpAddress -NotMatch 'DiscoverySearchMailbox' | Sort-Object UserPrincipalName
$distributiongroups = "Executives", "Sales Team", "Tailspin Toys"
foreach ($distributiongroup in $distributiongroups) {
$distributiongroupmembers = Get-DistributionGroupMember -Identity $distributiongroup
foreach ($user in $users) {
if (-not ($distributiongroupmembers | Select-String $user.name)) {
$output = [pscustomobject]@{
"DistributiongroupName" = $distributiongroup
"UserName which is not a Member" = $user.UserPrincipalName
}
$total += $output
}
}
}
$total | Export-Csv -Path c:\temp\distributiongroups.csv -NoTypeInformation -Delimiter ';'
CSV:
Apr 06 2022 02:09 PM
@Harm_Veenstra Thank you so much. I'll test it out and get back to you but this looks like it might do it for me!!!
Apr 06 2022 02:18 PM - edited Apr 08 2022 06:03 AM
No problem, let us know if it works out for you!
Apr 11 2022 01:05 AM - edited Apr 18 2022 08:52 AM
Did it work for you ?
May 14 2022 03:27 AM
May 16 2022 05:20 AM
May 16 2022 05:26 AM - edited May 16 2022 05:29 AM
You can use whatever filter you want 😉 I added the discoverymailbox because it was also evaluated, you can leave that out if you want.. Performance wise there was no impact in my test environment, very few users, but in larger environments it could take somewhat longer. But is this something you run once in a while or daily?
Shorter version:
May 17 2022 07:10 AM
Can't this all be done server side.
Get-Mailbox -Filter {$_.PrimarySmtpAddress -Match 'M365x537152.OnMicrosoft.com' -and $_.PrimarySmtpAddress -NotMatch 'DiscoverySearchMailbox'} |
Sort-Object UserPrincipalName
May 17 2022 07:12 AM
May 17 2022 07:23 AM
May 17 2022 07:28 AM - edited May 17 2022 08:01 AM
Ah, yes! You're right! But PrimarySmtpAddress doesn't look like something you can filter regarding Primary address Filterable properties for the RecipientFilter parameter | Microsoft Docs
May 17 2022 09:17 AM
May 17 2022 10:03 AM