Link to home
Start Free TrialLog in
Avatar of ITLady45
ITLady45

asked on

Powershell script to compare group membership for various users listed in txt file.

New to powershell.  
Is there a quick script someone can provide?  
Windows 2008 R2 DCS.
I have a list of users in a txt file.  I need to search the list of users in txt file and their group membership to see if there is a common group listed then export to CSV file.  
Any help would be greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$Result= @()
$Users = Import-Csv C:\Filename.csv

Foreach ($User in $Users){

$Mailbox = Get-Mailbox -Identity $User
$DL      = Get-DistributionGroup -ResultSize Unlimited | where { (Get-DistributionGroupMember $_ | foreach {$_.PrimarySMTPAddress}) -contains $User.PrimarySMTPAddress}

$Mbx = New-Object System.Object

$Mbx | Add-Member -MemberType NoteProperty -Name Displayname -Value $Mailbox.DisplayName
$Mbx | Add-Member -MemberType NoteProperty -Name DistributionName -Value $DL.Name

$Result +=$Mbx

}

$Result

Open in new window


Your CSV must have a user's PrimarySMTPAddress ad header mean:

Email
user01@contoso.com
user02@contoso.com
..................
.................
..................