We help IT Professionals succeed at work.

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

3,509 Views
Last Modified: 2017-03-29
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

Lead email system administrator
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions