Member_2_4371340
asked on
Powershell Query - Input Excel List of AD Security Groups -> Output Excel Sheet of Group Members
Hi,
I have an excel sheet with a single column of Active Directory security groups (40 in total). I want to run a powershell query that outputs the members of each group to an Excel sheet (Column1: groupName, Column2: memberName).
There are plenty of examples on the web on how to report on all groups in Active Directory, but we have thousands of groups, but I only want to report on 40 specific groups.
Help!
Fin
I have an excel sheet with a single column of Active Directory security groups (40 in total). I want to run a powershell query that outputs the members of each group to an Excel sheet (Column1: groupName, Column2: memberName).
There are plenty of examples on the web on how to report on all groups in Active Directory, but we have thousands of groups, but I only want to report on 40 specific groups.
Help!
Fin
Powershell has this nice cmdlet call import-csv. It'll take the listing of one column of data in a file (with a label) and dump it into a array. Then you just have to loop through that array.
For example, if you have the following LIST.CSV
Name
groupone
grouptwo
groupthree
For example, if you have the following LIST.CSV
Name
groupone
grouptwo
groupthree
foreach($Group in Import-Csv c:\list.csv)
{
Run your script using the variable.label
Write-Host $Group.Name
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh and if you change the line...
$Groupmembers= Get-QADGroupMember $GroupName
to
$Groupmembers= Get-QADGroupMember $GroupName -Indirect
Then it will also list the members who are indirect members of a group.
i.e. it will expand membership when a group contains a group (contains a group ... n )
$Groupmembers= Get-QADGroupMember $GroupName
to
$Groupmembers= Get-QADGroupMember $GroupName -Indirect
Then it will also list the members who are indirect members of a group.
i.e. it will expand membership when a group contains a group (contains a group ... n )
Finally, if you want a version that just creates a spreadsheet for ALL Members of ALL groups...
#Make ADSI Connection
#$Group = [ADSI]"LDAP://<domain>/<ldap_cn_of_group"
clear
#Setup our excel File
$Excel = new-object -comobject Excel.Application
$Workbook = $Excel.workbooks.open("c:\scripts\Book1.xls")
$worksheet2 = $Workbook.worksheets.add()
$Row = 2
#Create File and Header information
$Worksheet2.cells.item(1,1) = "Group"
$Worksheet2.cells.item(1,2) = "Display Name"
$Worksheet2.cells.item(1,3) = "SamAccountName"
$groups = Get-QADGroup -Empty:$false
foreach ( $group in $groups ) {
$GroupName = $Group.name
$Groupmembers= Get-QADGroupMember $GroupName -Indirect
foreach ($member in $Groupmembers)
{
$Worksheet2.cells.item($Row, 1) = $GroupName
$Worksheet2.cells.item($Row, 2) = $member.DisplayName
$Worksheet2.cells.item($Row, 3) = $member.SamAccountName
$Row++
}
}
$Workbook.save()
#$Excel.Visible = $true
$Excel.quit()
ASKER
Brilliant mod to script - Only had to modify line 5 to point the script at the input Excel sheet.
Thanks very much
Thanks very much
Glad to be of service.
ASKER
Open in new window
Credits to http://serverfault.com/questions/121944/how-to-export-ad-security-list-to-excel