Solved

Need a script  that gets group name and member

Posted on 2014-10-22
6
511 Views
Last Modified: 2014-10-22
Hello,
I need a script  that gets security group name and member information. I want to use excel file with security groups listed that I need to get members for. The export that I would like to get should include both group name and its members. I already tried the following, but it only gets the members without group name.

$Users = Import-CSV -Path C:\Temp\GroupsTest.csv

$Users | %{Get-ADGroupMember –identity $_.Group} | select name | export-csv c:\temp\SecurityGroupMembers10-22.csv

Below is the excel file I am using:
It has just one column "group" that lists the name of the security groups that I need to get members for.
Group:
Group1
Group2

I am new to powershell, so please explain in details what each line means.

I also tried the following already, but it doesn’t work:
Get-ADGroup -Filter {GroupCategory -eq "Security" -and Name -like "*"}| %{ $Group = $_.Group Get-ADGroupMember $Group | Select @{N="GroupName";E={$Group}},@{N="Member";E={$_.Group}}" }| Export-Csv C:\Users\Desktop\AD\Report.csv -nti Get-ADUser -Filter * -properties * | select fullname*, username*, disp*,distinguishedName*,GroupName,GroupCategory,Member
0
Comment
Question by:claudiamcse
  • 2
  • 2
  • 2
6 Comments
 

Author Comment

by:claudiamcse
Comment Utility
I get the error below: Unexpected token 'Get-ADGroupMember' in expression or statement.

 $Users | %{Get-ADGroup -identity $_.Group}| %{ $Group = $_.Group Get-ADGroupMember $Group
Unexpected token 'Get-ADGroupMember' in expression or statement.
At line:1 char:83
+ $Users | %{Get-ADGroup -identity $_.Group}| %{ $Group = $_.Group Get-ADGroupMember <<<<  $Group
    + CategoryInfo          : ParserError: (Get-ADGroupMember:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

$Users | %{Get-ADGroup -identity $_.Group}| %{ $Group = $_.Group Get-ADGroupMember $Group
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
You need to separate the different commands:

$Group = $_.Group;  Get-ADGroupMember $Group 

Open in new window


a semi-colon does this or add a new line.
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 250 total points
Comment Utility
Here is a shorter version that will perform the same thing

$Users = Import-CSV -Path C:\groups.csv
$Users | % {
$group = $_.Group 
Get-ADGroupMember –identity $group} | select @{Name="Group Name";Expression={$group}},@{Name="Member";Expression={$_.Name}} | export-csv c:\SecurityGroup.csv -nti

Open in new window


EDIT: Just noticed you wanted more member info, what information are you wanting?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Assisted Solution

by:becraig
becraig earned 250 total points
Comment Utility
Requested explanation of what the lines do:


#this line call into AD and queries for all groups that are of the type "Security" with ANY name "*"  then pipes it into a fireach loop ( | %)
Get-ADGroup -Filter { GroupCategory -eq "Security" -and Name -like "*" } | % {

#This next line assigns a variable of group to value $_.group retrieved from your query
     $Group = $_.Group;
#You then run a  query with get-adgroupmember to get all the members for each security group returned from the query above using the variable $group (you just defined as the name).  The you proceed to assign labels and values to a hash table before creating the output to your csv (Select @{ N = "GroupName"; E = { $Group } })    [This says $group will have a label / header of "Groupname"]

   Get-ADGroupMember  $Group | Select @{ N = "GroupName"; E = { $Group } }, @{ N = "Member"; E = { $_.Group } }

Finally you export your results to csv.
} | Export-Csv C:\Users\Desktop\AD\Report.csv -nti
0
 
LVL 16

Expert Comment

by:Joshua Grantom
Comment Utility
Here is one with more info.

$Users = Import-CSV -Path C:\groups.csv
$Users | % {
$group = $_.Group 
Get-ADGroupMember –identity $group} | select @{Name="Group Name";Expression={$group}},@{Name="Member Name";Expression={$_.Name}},@{Name="SamAccountName";Expression={$_.SamAccountName}},@{Name="Distinguished Name";Expression={$_.DistinguishedName}} | export-csv c:\SecurityGroup.csv -nti

Open in new window

0
 

Author Closing Comment

by:claudiamcse
Comment Utility
Thank you so much! Excellent!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

771 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

16 Experts available now in Live!

Get 1:1 Help Now