Help generating a report from Powershell

Hello Powershell Experts,

I think I have asked a similar question in the past, but I can't find it.  I have been tasked with finding all of the distribution groups in our domain and then getting a list of all the user in each group.  The following Powershell code gets the results I want and displays them to the screen in the format that I want:

$DistributionGroups = get-adgroup -filter 'GroupCategory -eq "distribution"' | sort samaccountname

foreach ($DistributionGroup in $DistributionGroups)
{
        Write-host $DistributionGroup.name
    Write-Host "---------------------------------------"
    Get-ADGroupMember -Identity $DistributionGroup.SamAccountName | select name | sort name
    Write-Host ""
}

My question is how can I get this information into a text or CSV file?
LVL 1
ndalmolin_13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

ITguy565Commented:
Easiest way I can think of to get it to txt is using the transcript command

start-transcript -path c:\temp\transcript.txt
DistributionGroups = get-adgroup -filter 'GroupCategory -eq "distribution"' | sort samaccountname

foreach ($DistributionGroup in $DistributionGroups) 
{
        Write-host $DistributionGroup.name
    Write-Host "---------------------------------------"
    Get-ADGroupMember -Identity $DistributionGroup.SamAccountName | select name | sort name
    Write-Host ""
} 
stop-transcript

Open in new window

0
oBdACommented:
Export to csv:
Get-ADGroup -Filter 'GroupCategory -eq "distribution"' |
	Sort-Object -Property SamAccountName |
	ForEach-Object {
		$groupName = $_.Name
		Get-ADGroupMember -Identity $_.SamAccountName |
			Sort-Object -Property Name |
			Select-Object -Property @{n='Group'; e={$groupName}}, Name
	} | Export-Csv -NoTypeInformation -Path C:\Temp\distribution.csv

Open in new window

0
ITguy565Commented:
If you want to export to csv this should do it :

$DistributionGroups = get-adgroup -filter 'GroupCategory -eq "distribution"' | sort samaccountname

$Groupcollection = @()
$groupobjects = ""|select Name, Member, DGMembers

foreach ($DistributionGroup in $DistributionGroups) {
    $groupobjects.name = $distributiongroup.name
    $groupobjects.DGMembers = (Get-ADGroupMember -Identity $DistributionGroup.SamAccountName |select name).name
        foreach ($member in $($groupobjects.DGMembers)){
            $groupobjects.Member = $member 
            $GroupCollection += $($groupobjects |select Name, Member)}
}
$groupcollection | export-csv -NoTypeInformation -Path c:\temp\test.csv

Open in new window

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
ndalmolin_13Author Commented:
Thanks for your help 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.