Export - Output Powershell command

Hi Experts,
I am using the below to get all the DGs and the owners of the DGs. I need to export the result into an excel sheet.

$dg= get-distributiongroup


and than


foreach ($item in $dg) {"Group: " + $item.name ; Write-output "-Members-"; Get-DistributionGroupMember $item}



I tired | Export-CSV c:\DistributionGroupMembers.csv and it didn't work.

thanks in advance
Ann AbedAsked:
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.

Dustin SaundersDirector of OperationsCommented:
So, that sort of thing doesn't really export into CSV well since its collections of collections.

What you could do is just write out your own CSV using Add-Content.

$output = "C:\Path\File.csv"

if (Test-Path $output) { Remove-Item $output }

Add-Content -Path $output -Value "Group,Member"

$distGroups = Get-DistributionGroup

foreach ($distGroup in $distGroups)
{
    Get-DistributionGroupMember $distGroup | %{ Add-Content -Path $output -Value "$($distGroup.Name),$($_.sAMAccountName)" }

}

Open in new window


Don't have an Exchange server to test but any problems with it, post back.
1

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
Ann AbedAuthor Commented:
Thank you so much, will test it and get back to you.
0
MilesLoganCommented:
depends where your groups are stored , you can try something like this also ..and also depends where you have the owner details.


get-adgroup -Filter 'GroupCategory -eq "Distribution"' -SearchBase 'OU DN HERE' -Properties name,ManagedBy |
Select-Object name,ManagedBy | Export-Csv DGDetails.csv -NoTypeInformation

Open in new window

1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ann AbedAuthor Commented:
Thanks MilesLogan.

I need a list of the groups as well as the members.
0
Ann AbedAuthor Commented:
Thank you, I ended up running 2 different scripts to get the above. Starting this week doing pwoershell courses as I see how important it is and how easy it can be if you know it.

thx everyone
0
Ann AbedAuthor Commented:
Thank you, I ended up running 2 different scripts to get the above. Starting this week doing pwoershell courses as I see how important it is and how easy it can be if you know it.

thx everyone
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.