I have some code so far, but would like it to result in a table with multiple lines for users per group.
Currently, it creates a table like this:
| Group | Users |
|---|---|
| abcgroup1 | Alice |
| abcgroup1 | Bob |
| abcgroup2 | Bob |
| abcgroup2 | Jason |
| abcgroup3 | Eve |
I would like it to instead create a table like this:
| Group | Users |
|---|---|
| abcgroup1 | Alice Bob |
| abcgroup2 | Bob Jason |
| abcgroup3 | Eve |
$Groups = get-adgroup -Filter 'name -like "*abc*"'
$Results = foreach( $Group in $Groups ){
Get-ADGroupMember -Identity $Group | foreach {
[pscustomobject]@{
Group = $Group.Name
Users = $_.Name
}
}
}
$Results
$Results | Export-Csv C:\abc_search.txt -NoTypeInformation
You can use
-joinoperator concatenating by CRLF`r`n. This will result in a multilinestring:Note that I'm using
-WraponFormat-Table, needed to correctly display multilinestringson the console.The other option you could use is
Out-Stringthought that would also require the use of.TrimEnd()method to get rid of the trailing new line: