2

I have the below script pulling the correct information, but need assistance formatting it. What I am looking for is a table that lists the Group names in Column A and the User names in Column B. The output right now lists the group names in Column A, but puts the user names all on one line in Column B.

This is what comes out:

Col A      Col B

Group1     {User 1, User 2, User 3}

This is what I would like to see:

Col A      Col B

Group 1    User 1

           User 2

           User 3

Here is the script I have thus far.

Get-QADGroup -Name '*Group Name*' | ForEach-Object {

$group = $_
$member = $group | Get-QADGroupMember -IncludedProperties samaccountname -UseDefaultExcludedProperties $true -SizeLimit 0

New-Object -TypeName PSObject -Property @{
    Name = $group.Name
    EID = $member
    }
}
1
  • I don't think that there is another way. {User 1, User 2, User 3} is the normal formatting output. Each line is a new entry and for group1 is only one entry there. That's the reason why it is all on one line Commented May 6, 2013 at 15:02

1 Answer 1

1

Not the format you asked for, but will group the members by group: (Updated to include SamAccountName)

Get-QADGroup -Name '*Application*' | `
% { $group = $_.Name; $_; } | ` 
Get-QADGroupMember | `
Select-Object @{n='Group';e={$group}}, Name, SamAccountName | `
Sort-Object Group | `
Format-Table Name, SamAccountName -GroupBy Group

If you really need the format exactly as asked for, one way you could get it is to order, then keep track of the previous group value, and if it is the same, mark it as empty, something like:

Get-QADGroup -Name '*Application*' | `
% { $group = $_.Name; $_; } | ` 
Get-QADGroupMember | `
Select-Object @{n='Group';e={$group}}, @{n='Member';e={$_.Name}} | `
Sort-Object Group | `
ForEach-Object {

$currentGroup = $_.Group

if ($currentGroup -eq $previousGroup)
{
 $_.Group = [string]::Empty
}

$previousGroup = $currentGroup
Write-Output $_
} | Format-Table
Sign up to request clarification or add additional context in comments.

3 Comments

The top one works beautifully. One more question. I need the sAMAccount Name to appear on the list with the actual user name. How can I add that field into this?
Figured it out. Formatted the last little bit as "@{n='EID'; e={$_.sAMAccountName}} | Sort-Object Group | Format-Table EID , Member -GroupBy Group". Thank you for your help.
Updated first example to include SamAccountName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.