1

New to powershell, trying to understand how things work/syntax - after scavaging microsoft docs and other postings I can't find a direct solution/proper code to get what I am looking for.

Get-Group | Select DisplayName, Members

This simply outputs two columns for me, all the groups... and all the members of each group. (300 mailboxes - and a lot of users)

When exported as CSV - the members become a string without any split/seperator outside of whitespace. This causes an issue because I am not able to sort based off member names.

the output in powershell itself would look like this for the members column:

{john doe, becky boo, bob smith...}
{ryan john, brianna nuggets, pizza pie...}

etc... goes on for the 300 mailboxes, and of course when exported itll show the full list of members.

Can someone show me how exactly I am supposed to access each individual name for each group? For example... for group1 - output EACH individual name.

output:

John Doe
Becky Boo
Bob Smith

Typically in other languages it would be something like members[0][0]would output the first ArrayList, first element... no idea how its done in powershell and can't find proper documentation for it.

Either a good direction to be pointed towards, an explanation on how to properly iterate over the object list, or store specific values... all information is helpful as I am just looking for learning material since I am doing it for fun anyway. Thanks in advance!

Edit:

Was able to output all groups & members to excel - allowing me to filter it properly with any method I choose (All members belonging to xxx group, or what groups xxx member belongs to.)

$results=@()
$groups = Get-group -resultsize unlimited | sort displayname
$groups | ForEach-Object {
    $group=$_
    $group.members | foreach-object {
        $member = $_
        $results += New-Object PSObject -property @{
            Member = $member
            GroupName = $group.DisplayName
            MailboxType = $group.RecipientTypeDetails
            GroupType = $group.GroupType
            ManagedBy = $group.ManagedBy
        }
    } 
}
$results | export-csv -path c:\All_Groups.csv

1 Answer 1

1

Will print the group name of the first group followed by all the users in it, remove or change -first 1 if u want more users:

Get-Group | Select -first 1 -properties DisplayName, Members | % {
    write-host $_.DisplayName -ForeGroundcolor Green
    $_.Members | % {write-host $_.DisplayName}
}

Or for some different syntax:

foreach ($group in Get-Group | Select -first 1 -properties DisplayName, Members) {
    write-host $group.displayname 
    foreach ($member in $group.members) {
        write-host $member.displayname -ForeGroundcolor Green
    }
}

How to access attributes:

$groups = get-group
$groups[0].DisplayName
$groups[0].Members[0].DisplayName

export-csv only works well for 1 dimension arrays. If u use 2D or more arrays then use something like Export-Clixml / import-clixml

Alternatively u could export manualy to a csv like this:

"groupDisplayName,members" | out-file -path ./export.csv 
foreach ($group in Get-Group) {
    $groupname_with_added_comma = $group.displayname + ","
    $all_members_seperated_with_dot_comma = ''
    foreach ($member in $group.members) {
        $all_members_seperated_with_dot_comma += $member.displayname + ";"
    }
    $line_to_appen_to_file = $groupname_with_added_comma + $all_members_seperated_with_dot_comma
    $line_to_appen_to_file | out-file -path ./export.csv -append
}

or in a difrent way:

"groupDisplayName,members" | out-file -path ./export.csv 
Get-Group | % {
    $groupname_with_added_comma = "$($_.displayname),"
    $all_members_seperated_with_dot_comma = ($_.members | select -expandproperty displayname) -join ";"
    $line_to_appen_to_file = $groupname_with_added_comma + $all_members_seperated_with_dot_comma
    $line_to_appen_to_file | out-file -path ./export.csv -append
}
Sign up to request clarification or add additional context in comments.

3 Comments

Absolutely awesome feedback.. Going over the info now, I also managed to get the exact output I was looking for to be able to filter through all groups & members that belong to those groups. - Please check out the new code I added and let me know what best practices/changes you might make.
Your result looks really nice, i like how u created your custom objects. I have some personal preference for creating, I like to do it like this. [pscustomobject]@{someproperty='somevalue'} instead off New-Object PSObject -property @{someproperty='somevalue'} But yours is as good as my way.
I also like to use % instead of foreach-object, but its personal also, % is short, foreach-object is better readable also for users whom are not familiar with powershell.

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.