powershell select multiple fields using -expandproperty and arrange in columns exchange 2010

techdrive
techdrive used Ask the Experts™
on
Good Evening I need some assistance please. I am using the following command below

get-distributiongroup -identity "groupname" | select managedby -expandproperty | select name

Now this command is great as it gives me just the name without the long path. I was trying to pull another value like the name of the group in a foreach like

This fails when trying to pull multiple fields
get-content c:\nameofdlgroups.csv | foreach { get-distributiongroup -identity $_ } | select managedby -expandproperty , displayname| select name, displayname

My end goal is to get the output to display like this below in a csv file. Its just hard displaying the name of the group plus the multiple owners of the group.

group name
groupmanagedbymember23
groupmanagedbymember13
groupmanagedbymember73
groupmanagedbymember012

group name1
groupmanagedbymember23
groupmanagedbymember13
groupmanagedbymember73
groupmanagedbymember012

group name2
groupmanagedbymember23
groupmanagedbymember13
groupmanagedbymember73
groupmanagedbymember012

group name3
groupmanagedbymember23
groupmanagedbymember13
groupmanagedbymember73
groupmanagedbymember012
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Lead email system administrator
Commented:
get-content c:\nameofdlgroups.csv |get-distributiongroup  | select displayname -expandproperty managedby | select displayname,name | fl name -GroupBy Displayname

or if you need some formatting after

$MyList=get-content "c:\nameofdlgroups.csv" |get-distributiongroup  | select displayname -expandproperty managedby | select displayname,name

foreach($MyItem in $MyList){

if ($MyprevItem -ne $MyItem.DisplayName){
$MyItem.DisplayName
"             $($MyItem.Name)"
$MyprevItem=$MyItem.DisplayName
} else{
"             $($MyItem.Name)"
}
}
Chris DentPowerShell Developer
Top Expert 2010
Commented:
Just an alternative since you mentioned CSV output.
get-content c:\nameofdlgroups.csv | 
    foreach { get-distributiongroup -identity $_ } |
    Select-Object displayname, @{Name='ManagedBy';Expression={ $_.ManagedBy -join "`n" }}
    Export-Csv someFile.csv

Open in new window

The managedBy property is joined together using a line break ("`r`n" is a reasonable alternative). You can join using anything you like, this is only one approach that lets you push it to a file using the built in commands.

Author

Commented:
Works great Pavel but having issues saving into a car using  export-csv

Author

Commented:
I think this might work and thanks also Chris Dent


$OutFile = $output = "c:\temp\gmembers.txt"
$MyList=get-content "c:\nameofdlgroups.csv" |get-distributiongroup  | select displayname -expandproperty managedby | select displayname,name

output = `
 foreach($MyItem in $MyList){

 if ($MyprevItem -ne $MyItem.DisplayName){
 $MyItem.DisplayName
 "             $($MyItem.Name)"
 $MyprevItem=$MyItem.DisplayName
 } else{
 "             $($MyItem.Name)"
 }
 }
$Output | Out-File $OutFile -Append

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial