Avatar of Tom Conklin
Tom Conklin
Flag for United States of America asked on

Exchange Powershell script to find users NOT in a group

Hi,
I'm looking for the following (Exchange 2010):
List all users that have mailboxes but are not in a group called Metalogix*
We have a bunch of groups called metalogix_sitenames, so I would like to use a wildcard...
Thanks in advance!
Tom
PowershellExchange

Avatar of undefined
Last Comment
Will Szymkowski

8/22/2022 - Mon
Will Szymkowski

I will be able to help shortly.

Will.
Will Szymkowski

Here you go...
Import-module activedirectory
$Users = Get-ADUser -Filter * -Properties sAMAccountName
$Group = "Export*"

ForEach ($user in $Users)
    {
  
        $Check = Get-ADPrincipalGroupMembership -Identity $User.sAMAccountName | ? { $_.Name -like $Group } 
              
        If ($Check -eq $null)
            {
                Write-Output "$User.sAMAccountName is NOT part of this group"
            
            }
    
       else {
       
                $Results = Get-ADUser -Identity $User.sAMAccountName -Properties Name, sAMAccountName, Mail | 
                           select Name, sAMAccountName, Mail |
                           Export-csv "c:\results1.csv" -NTI -Append
       
            }
    
 }

Open in new window


Will.
Tom Conklin

ASKER
Will - This brought back quite a few users that DID belong to the groups (changed $Group = "Export*" to "Metalogix*"
This also used AD - I was looking more for the script to qualify based on if a mailbox existed. Is this possible?
Thanks!
Tom
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Will Szymkowski

This brought back quite a few users that DID belong to the groups

Not sure how that would be happening. I have tested this in my lab and it works perfectly. Only providing me with the users that were part of the Export Groups.

However if you want to read all of the group memberships that a user is part of then you need to use the Get-ADPrincipalGroupMemberShip cmdlet which is an AD cmdelt.

I can then change the $Results variable to a Exchange cmdlet like Get-mailbox and export the results rather than using Get-ADUser.

See below...

Import-module activedirectory
$Users = Get-Mailbox -ResultSize "unlimited" 
$Group = "Metalogix*"

ForEach ($user in $Users)
    {
  
        $Check = Get-ADPrincipalGroupMembership -Identity $User.sAMAccountName | ? { $_.Name -like $Group } 
              
        If ($Check -eq $null)
            {
                Write-Output "$User.sAMAccountName is NOT part of this group"
            
            }
    
       else {
       
                $Results = Get-Mailbox -Identity $User.sAMAccountName | 
                           select Name, sAMAccountName, PrimarySmtpAddress, Database |
                           Export-csv "c:\results1.csv" -NTI -Append
       
            }
    
 }

Open in new window


Will.
Will Szymkowski

Just one question. Are these groups are you talking about only Distribution Groups or Mail Enabled Security Groups? If they are specifically Security Groups only then you need to use the Active Directory module.

If they are distribution groups then i might be able to do this completely in Exchange.

Will.
Tom Conklin

ASKER
Sorry for not getting back to you. I tried to run the script above on a CAS server, and it hammered the performance, so I canceled. The groups are global security, but I would want it to only look for users that have a mailbox. Can this be done with a AD PS, or would it need to be the Exchange PS?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Will Szymkowski

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tom Conklin

ASKER
Thanks for staying with this Will - It ended up being EXACTLY what I needed!
Will Szymkowski

EXCELLENT, glad I could help!

Will.