MilesLogan
asked on
Powershell - How to check if a set of users are members of certain groups.
Hi EE
This is a weird issue even describing .. Does anyone have a way of doing the task below .
I have a set of users .. 2500 .. and I need to know which users are members of a set of 5 groups ..
any ideas ?
This is a weird issue even describing .. Does anyone have a way of doing the task below .
I have a set of users .. 2500 .. and I need to know which users are members of a set of 5 groups ..
any ideas ?
What type of result file are you looking for? Do you need to report each group membership status (True false against each group) or just report if they are missing any group? or do you need to add them to group if they are missing the group membership?
See if this works for you:
$Groups = Get-Content C:\GroupsList.txt
$Users = Get-Content C:\UserList.txt
$MembersArray = @()
foreach ( $User in $Users ) {
foreach ( $Group in $Groups ) {
$memberOf = Get-QADMemberOf -Identity $User -Name $Group
if($memberOf)
{
$MembersArray += $User
}
}
}
$MembersArray | Export-CSV C:\ListOfMembers.csv -NoType
ASKER
Hi YZlat
I modified the path to point to where I have the user and group list and the outout is listed below ..
PSPath PSParentPath PSChildName PSDrive PSProvider ReadCount Length
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 1 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 1 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 1 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 2 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 2 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 2 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 2 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 2 8
E:\Projects\Groups\Users.t xt E:\Projects\Groups Users.txt E Microsoft.PowerShell.Core\ FileSystem 2 8
I modified the path to point to where I have the user and group list and the outout is listed below ..
PSPath PSParentPath PSChildName PSDrive PSProvider ReadCount Length
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
E:\Projects\Groups\Users.t
@MilesLogan, Could you please clarify my above question?
ASKER
Hi SubSub .. I just need to know if they are a member of any of those groups .. I just need the SAmAccountName and which group they where a member of in the output file .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
could you show me the sample content of Users.txt and Groups.txt?
ASKER
Worked perfect ! thanks !
Here is a topic on array initialization with strings and going through it using a for loop: http://get-powershell.com/post/2008/02/07/Powershell-function-New-Array.aspx
Here is a topic on how to check user membership in a group: http://social.technet.microsoft.com/Forums/scriptcenter/en-US/1e75ab95-fd52-4eb5-a1c3-136d14050b63/check-if-specific-user-is-member-of-specific-group
There are many more articles on the topics in google, so you just need to combine several to reach your solution.