Solved

Get-Adgroupmember

Posted on 2016-09-14
11
43 Views
Last Modified: 2016-09-14
I will like to import a bulk  of AD groups, and get the all the members that are locating inside of it.  Can some provide me the correct syntax for this.  I will like to use the Foreach Cmdlt to do this work.
0
Comment
Question by:JCJohnson76
  • 6
  • 4
11 Comments
 
LVL 16

Expert Comment

by:FOX
ID: 41798281
List the names of your groups and at the top put a header named Groups and name it groups.csv

Import-Csv 'c:\filelocation\groups.csv' | Foreach{Get-ADGroupMember -identity $_.Groups -recursive | select name | Export-csv -path 'C:\filelocation\Groupmembers.csv' -NoTypeInformation
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41798282
Try..
Import-Module Activedirectory
Get-ADGroup -F * | %{$name = $_.Name; $_ | Get-ADGroupMember -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\Groupreport.csv -nti

Open in new window

Result will have all groups and it's members in CSV format. If you want to filter the members or groups from result that also can be done..
0
 

Author Comment

by:JCJohnson76
ID: 41798356
Import-Csv 'c:\filelocation\groups.csv' | Foreach{Get-ADGroupMember -identity $_.Groups -recursive | select name

This is only displaying the user list that is located in the groups.  I will like for it to display user list and the Group Name the user is located inside
0
 
LVL 40

Expert Comment

by:Subsun
ID: 41798376
@JCJohnson76, Did you check the code which I posted?
0
 

Author Comment

by:JCJohnson76
ID: 41798386
Subsun,

That code provide a broad scope of all groups.  The code I'm looking for is where I specify a range of  groups listed in a csv file.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 40

Expert Comment

by:Subsun
ID: 41798402
You didn't mention, you need to read from a input file.. :-)
Try this modified code..
Import-Module Activedirectory
Import-csv C:\input.csv | %{$name = $_.GroupName; Get-ADGroupMember $_.GroupName -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\Groupreport.csv -nti

Open in new window


Sample input CSV
GroupName
GroupA
GroupB

Open in new window

0
 

Author Comment

by:JCJohnson76
ID: 41798429
And yes I populated the csv file with the correct header

Received a bunch of these errors

Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null
or empty, and then try the command again.
At line:2 char:78
+ Import-csv C:\csv\fin_groups.csv | %{$name = $_.GroupName; Get-ADGroupMember $_. ...
+                                                                              ~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember
0
 

Author Comment

by:JCJohnson76
ID: 41798435
Subsun
this is the code that i used which you provided

Import-Module Activedirectory
Import-csv C:\csv\fin_groups.csv | %{$name = $_.GroupName; Get-ADGroupMember $_.GroupName -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\csv\Groupreportt.csv -nti
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41798496
Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null
Means the input value is null, probably the input file has some trailing spaces..


Import-csv C:\input.csv | ?{$_.GroupName}|%{$name = $_.GroupName; Get-ADGroupMember $_.GroupName -Recursive | Select @{N="Group";E={$name}},@{N="Member";E={$_.Samaccountname}},ObjectClass} | Export-csv C:\Groupreport.csv -nti

Open in new window

0
 

Author Comment

by:JCJohnson76
ID: 41798546
Subsun,

Thanks, Horray,

Works like a charm
0
 

Author Closing Comment

by:JCJohnson76
ID: 41798547
Outstanding!!!!!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

15 Experts available now in Live!

Get 1:1 Help Now