Avatar of JCJohnson76
JCJohnson76
 asked on

Get-Adgroupmember

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.
Powershell

Avatar of undefined
Last Comment
JCJohnson76

8/22/2022 - Mon
FOX

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
SubSun

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..
JCJohnson76

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SubSun

@JCJohnson76, Did you check the code which I posted?
JCJohnson76

ASKER
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.
SubSun

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
JCJohnson76

ASKER
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
JCJohnson76

ASKER
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
ASKER CERTIFIED SOLUTION
SubSun

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.
JCJohnson76

ASKER
Subsun,

Thanks, Horray,

Works like a charm
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
JCJohnson76

ASKER
Outstanding!!!!!