Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

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?
Avatar of ITguy565
ITguy565
Flag of United States of America image

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

Avatar of oBdA
oBdA

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

ASKER CERTIFIED SOLUTION
Avatar of ITguy565
ITguy565
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ndalmolin_13

ASKER

Thanks for your help everyone.