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

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
techdriveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pavel NagaevLead email system administratorCommented:
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)"
}
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris DentPowerShell DeveloperCommented:
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.
0
techdriveAuthor Commented:
Works great Pavel but having issues saving into a car using  export-csv
0
techdriveAuthor 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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.