Solved

Help getting an expanded list of users in a group with Powershell

Posted on 2013-12-02
4
654 Views
Last Modified: 2013-12-02
Hello Powershell Experts,

I would like to see a list of all users that are in our various sales groups.  This part is easy enough.  I used the following Powershell code to generate this list.

$SalesGroups = Get-ADGroup –filter  ‘name –eq “Sales*”’
Foreach ($SalesGroup in $SalesGroups) {
      Get-ADGroupMember  -identity $SalesGroup  | Select name, objectclass
}

This produces a list of group members and lets me know whether each group member is an actual user or a group.  

Name                              Objectclass
BobS                              user
LindaQ                              user
PhxTeam                        group
PeteH                              user

Now I want to go through this list and if a group member’s object class equals user, I want to add their name to a variable or array.  If the group member’s object class equals group, I want to get a list of users in that group and add them to the variable or array.  I’m hoping someone can help me with this part.

Thanks,
Nick
0
Comment
Question by:ndalmolin_13
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39690144
Are you trying to get the recursive group membership? If yes you can try..
$SalesGroups = Get-ADGroup –filter  ‘name –eq “Sales*”’
Foreach ($SalesGroup in $SalesGroups) {
  Get-ADGroupMember -identity $SalesGroup -Recursive  | Select @{N="Group";E={$SalesGroup.Name}},name,objectclass
}

Open in new window

0
 
LVL 1

Author Comment

by:ndalmolin_13
ID: 39690283
That did it.  Thanks for your help.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39690497
Nice!.. If you didn't require any further assistance then don't forget to close the question.. :-)
0
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 39691635
Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now