3

I am relatively new to powershell and am trying to get a list of all groups in the domain with their respective members. Here's what I'm working with now:

Import-Module ActiveDirectory
$OutPutFile = New-Item -type file -force "AllGroups.txt"

#Filters for Groups

# 2 Global distribution group
# 4 Domain local distribution group
# 8 Universal distribution group
# -2147483646 Global security group
# -2147483644 Domain local security group
# -2147483640 Universal security group

$D = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$Domain = [ADSI]"LDAP://$D"
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.PageSize  = 10000

$Searcher.Filter = "(&(objectCategory=Group)(|(groupType=2)(groupType=4)(groupType=8)))"
$Searcher.SearchRoot = "LDAP://" + $Domain.Name

$Results = $Searcher.FindAll()
foreach ($Result in $Results)
{
    $Item = $Result.Properties

    foreach ($Member in $Item.member) 
    {
    $Name = Get-ADUser "$Member" -Properties DisplayName | select -expand displayname
    $Item.cn, "$Name" | Out-File $OutPutFile -encoding ASCII -append
    }
}

I'd like the output to be Group;Displayname or Group-Displayname (so I can put it in Excel and format from there) however I cannot seem to get output in the same line. The output currently comes out like this:

Group
DisplayName
Group
DisplayName

What am I doing wrong or how can I adjust the code to give me the two items on the same line? I was also thinking about adding in the OU the groups are in but haven't had luck finding a way to get that from my results.

2
  • what version of powershell are you using? Have you looked at Get-ADGroupMember or the memberof property of get-aduser?
    – Colyn1337
    Nov 4, 2015 at 19:41
  • It's the powershell included in Windows 2008 R2, not sure of the version. I did try initially to use the get-aduser but it outputs the opposite way.
    – bg.ranken
    Nov 4, 2015 at 19:54

2 Answers 2

3

When you pipe a list to Out-File -Append, it'll write each entry to a new line.

Instead, construct a string from the two values and pipe that:

$MemberLine = '"{0}","{1}"' -f $Item.CN,$Name 
$MemberLine | Out-File $OutPutFile -encoding ASCII -append

Alternatively, use Export-Csv, it'll take care of comma-separating the values correctly:

$Members = foreach ($Result in $Results)
{
    $Item = $Result.Properties

    foreach ($Member in $Item.member) 
    {
        Get-ADUser "$Member" -Properties DisplayName |Select @{Name='Group';Expression={$Item.CN}},@{Name='Member',Expression={$_.displayname}}
    }
}
$Members | Export-Csv $OutPutFile -Encoding ASCII -NoTypeInformation

As already mentioned, you can replace your [adsisearcher] with the Get-ADGroup cmdlet, much more concise:

$Groups = Get-ADGroup -Filter {GroupCategory -eq 'Distribution'}
$Groups | ForEach-Object {
  $Group = $_
  $Group.Member |ForEach-Object {
    Get-ADUser $_ -Properties displayName
  } | Select @{N='Group';E={$Group.Name}},@{N='Member';E={$_.displayName}}
} | Export-Csv $OutPutFile -Encoding ASCII -NoTypeInformation
4
  • That top one with memberline is what I was looking for. However the output comes out like this:
    – bg.ranken
    Nov 6, 2015 at 22:07
  • @bg.ranken update your question with the erroneous output Nov 6, 2015 at 22:13
  • That top one with memberline is what I was looking for. The middle script seems to work well, however using both the output comes with the group blank: "","APC UPS Management Account" The last one seems to just output an empty file.
    – bg.ranken
    Nov 6, 2015 at 22:18
  • @bg.ranken Use $Item.Name instead of $Item.CN Nov 10, 2015 at 20:33
0

Just use following PowerShell code:

Get-ADGroup -filter "name -like 'CWS*'" | % {
$Group = $_.Name $_ | Get-ADGroupMember | select @{n="Group";e={$Group}}, 
@{n='Users';e={$_.Name}} | Export-CSV c:\temp\report.csv -NoTypeInformation -Append -Delimiter ";" }
2
  • When I run this I get the following error: Unexpected token '' in expression or statement. At C:\Users\user\Documents\GetGroups5.ps1:2 char:20 + $Group = $.Name $_ <<<< | Get-ADGroupMember | select @{n="Group";e={$Group}}, + CategoryInfo : ParserError: (_:String) [], ParseException + FullyQualifiedErrorId : UnexpectedToken
    – bg.ranken
    Nov 5, 2015 at 19:47
  • I don't know which PowerShell version you are using, but I have this one installed - microsoft.com/en-us/download/details.aspx?id=48729 Also try to remove line breaks and write code above as a single line, and if you want to get info about all groups you need to change filter to: Get-ADGroup -filter * Nov 5, 2015 at 23:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .