1

I have a need to get all of the AD groups and their respective memberships out of a specific AD OU. The code I have does that when writing to the host screen but I am not able to output it cleanly to .xml, which was the requested format, but at this point I will take it in any format. Management would like output file similar to :

Group1 User1
Group1 User2
Group1 User3 
Group2 User1
Group2 User2
Group2 User3
etc...

Anyone have any ideas?

$groupName = Get-ADGroup -SearchBase "OU=Groups,DC=domain,DC=com" -Filter * | Select-Object sAMAccountName

foreach ( $groupMember in $groupName )
{
   # $groupMember
   Get-ADGroupMember -Identity $groupMember.sAMAccountName | Export-Clixml -Path C:\temp.xml -Force
ekad
  • 14,436
  • 26
  • 44
  • 46
jam051
  • 31
  • 1
  • 3

2 Answers2

1

With the assumption that you want the two column text file like you displayed, which is not an XML formatted file. You could use a calculated property using Select-Object then export that information with the formatting of choice:

$GroupName = Get-ADGroup -SearchBase "OU=Groups,DC=domain,DC=com" -Filter * | Select-Object -ExpandProperty sAMAccountName

$Members = foreach ($GroupMember in $GroupName) {
   Get-ADGroupMember -Identity $GroupMember | Select-Object @{Name="Group";Expression={$GroupMember}},name
}
"$(Members.Group) $($Members.Name)"  | Out-File C:\temp.txt
#Alternatively for a .CSV
$Members | Export-CSV C:\temp.csv
BenH
  • 9,766
  • 1
  • 22
  • 35
0

i believe you were missing $ in front of "(Members.Group)" line 5 chr 3 shouldn't it be "$($Members.Group)" instead? because when i ran this script it gave me error 'The term 'Members.Group' is not recognized as the name of a cmdlet, function....'

$GroupName = Get-ADGroup -SearchBase "OU=Groups,DC=domain,DC=com" -Filter * | Select-Object -ExpandProperty sAMAccountName

$Members = foreach ($GroupMember in $GroupName) {
   Get-ADGroupMember -Identity $GroupMember | Select-Object @{Name="Group";Expression={$GroupMember}},name
}
"$($Members.Group) $($Members.Name)"  | Out-File C:\temp.txt
#Alternatively for a .CSV
$Members | Export-CSV C:\temp.csv
Bartek
  • 3
  • 3