How to list users under multiple groups and users sub groups

Hi, I am stump, which is not hard to do. i have a list of groups and i want to list the users in those groups and then in the next column lists all the citrix only groups for each user. hopefully im describing that correctly. Heres what i have but it is not listing the users groups. I am not sure how to proceed.

$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')

$Groupname = "Distribution Lists"

$excel = New-Object -comobject Excel.Application
$excel.visible = $True
$wbook = $excel.Workbooks.Add()

$wsheet = $wbook.Worksheets.Item(1)
$wsheet.Cells.Item(1,1) = "Groupname"
$wsheet.Cells.Item(1,2) = "Member"
$wsheet.Cells.Item(1,3) = "ACID"
$wsheet.Cells.Item(1,4) = "Department"

$range = $wsheet.UsedRange
$range.Interior.ColorIndex = 19
$range.Font.ColorIndex = 11
$range.Font.Bold = $True
$intRow = 2



$groups = get-adgroup -Filter * -properties * -Searchbase "OU=Citrix,OU=Permission,OU=Groups,OU=Home Office,OU=domain,DC=Domain,DC=com"
$targetFile = "c:\temp\$groupname $CurrentDate.csv"
Add-Content $targetFile "Group;Member;ACID;Department"


foreach ($group in $groups){
       $groupMembers = get-adgroupmember $group -Recursive | Get-ADUser -Properties Department, DistinguishedName| Where-Object { $_.Enabled -eq 'True' } | Select-Object Name, samaccountname, department, distinguishedname, @{n='MemberOf';e={$_.MemberOf -replace '^cn=([^,]+).+$','$1' -join '; '} 
       
      
       foreach ($groupMember in $groupMembers){
              
             
              
	      $groupName = $group.Name
              $memberName = $groupMember.Name
              $acid = $groupMember.samaccountname
	      $groups = $usergroups
          #$department = $groupMember.department
              $department = $groupMember.memberof
              #$DistinguishedName = $gropmember.distinguishedname
	      $line = "($groupName)--------($memberName)-----($acid)-------($department)------($usergroups)"
	      add-content $targetFile $line

	      $wsheet.Cells.Item($intRow,1) = $groupName
	      $wsheet.Cells.Item($intRow,2) = $memberName
	      $wsheet.Cells.Item($intRow,3) = $acid
	      $wsheet.Cells.Item($intRow,4) = $groups
	      $wsheet.Cells.Item($intRow,5) = $DistinguishedName
	      $intRow++
}
}
$WorkBook.EntureColumn.AutoFit()

$excel.SaveAs("DL" + "name.xlsx") 

$excel.Close()

April 28th, 2015 2:06pm

Sorry, but your question doesn't make any sense.

I would add that you need to work out exactly what information you are seeking before you start trying to do anything in Excel. That only adds unnecessary complexity into the situation.

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 2:10pm

Sorry, but your question doesn't make any sense.

I would add that you need to work out exactly what information you are seeking before you start trying to do anything in Excel. That only adds unnecessary complexity into the situ

April 28th, 2015 3:18pm

What is the purpose of this report?

Keep in mind that this forum is for asking specific scripting questions rather than for requesting scripts to

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 3:20pm

I understand, the purpose of the report is to  list what citrix groups users have by field office groups. at the basic level i am trying to list all users in a group and all the groups that user is in. I tried to pipe get-adgroupmember to get-aduser to get-adprincipalgroupmembership but that did not work.
April 28th, 2015 4:20pm

I'm not sure what you need exactly.

I would recommend searching the web and the repository for possible examples.

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 4:29pm

that's what i have been doing. i will keep searching. Thanks.
April 29th, 2015 8:10am

I think it's unlikely you will find a script that meets your exact specifications.

You will need to read the PowerShell documentation and understand how it uses the pipeline and processes objects.

Unfortunately there is no substitute for learning the basics. There is a good Learn link at the top of this forum to help you get started.

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 8:44am

Hi glacket,

First of all you are already complicating things by making the output to excel.

You should initially figure out what gives you the correct output using simplest possible code, not necessarily in proper format.

Once you have that, then you can go ahead and format Or maybe we can help you in that.

How about you come up with that.

If I have understood your requirement correct its as below:

You have groups Group1, Group2, ..... GroupN

The Members of the Group are various combination of User1, User2,..... UserY

Now you have CitrixGroups1, ........ N, with again the same set of users as members.

You are trying to find out the Users who are member of both Groups and list them.

The output format you have is inefficient as Eg.

You have already listed user1 in:

group1 | user1 | citrix-app1; citrix-app2; citrix-app3

Then again you are listing the same user1 in:

group2 | user1 | citrix-app1; citrix-app2; citrix-app3

April 29th, 2015 8:50am

Hi Glacket,

Below codes should give you headsup.

This command will give you estimate the result count for each group:

PS C:\Users\Administrator> Get-ADGroup -Filter {Name -like "TestGroup*"} | Select Name, @{Expression={get-adgroupmember $_  -recursive | Measure | Select -ExpandProperty Count};Label="Count"}

Name                           Count
----                           -----
TestGroup1                     7
TestGroup2                     8

Note that as said earlier we are getting duplicated results(12,13,14) for users belonging to multiple groups. Result is in order as per earlier code's count and order.

Get-ADGroup -Filter {Name -like "TestGroup*"} | get-adgroupmember  -recursive | Select Name,@{Expression={Get-ADPrincipalGroupMembership $_ | Select -ExpandProperty Name};Label="GroupMemberOfName"}

Name                                                        GroupMemberOfName
----                                                        -----------------
User100                                                     {Domain Users, TestGroup1}
User14                                                      {Domain Users, TestGroup1, TestGroup2}
User13                                                      {Domain Users, TestGroup1, TestGroup2}
User12                                                      {Domain Users, TestGroup1, TestGroup2}
User11                                                      {Domain Users, TestGroup1}
User10                                                      {Domain Users, TestGroup1}
User1                                                       {Domain Users, TestGroup1}
User19                                                      {Domain Users, TestGroup2}
User18                                                      {Domain Users, TestGroup2}
User17                                                      {Domain Users, TestGroup2}
User16                                                      {Domain Users, TestGroup2}
User15                                                      {Domain Users, TestGroup2}
User14                                                      {Domain Users, TestGroup1, TestGroup2}
User13                                                      {Domain Users, TestGroup1, TestGroup2}
User12                                                      {Domain Users, TestGroup1, TestGroup2}

Use below to export to CSV:

Get-ADGroup -Filter {Name -like "TestGroup*"} | get-adgroupmember  -recursive | Select Name,@{Expression={Get-ADPrincipalGroupMembership $_ | Select -ExpandProperty Name};Label="GroupMemberOfName"} | Export-Csv C:\ListGroups.csv

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 1:31pm

Hi Glacket,

Any Updates.

May 4th, 2015 7:55am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics