Powershell and Exchange 2007

Associate
Joined
13 Jun 2005
Posts
1,416
Location
West Midlands
Good Afternoon,

I am trying to do the following -

  • Run off a report of all the distribution lists and mail enabled security groups
  • List the members of each group alongside each DL SG

I have been using the following powershell script -

**NOTE: I am a complete noob when it comes to Powershell.**

# dump all DLs to a variable, whether DistributionList or mail-enabled Sec-group
$DLList = get-distributionlist
# Iterate over each DL to get member-list
foreach ($DL in $DLList) {
# Get the member list
$Dmember=get-distributiongroupmember $DL
# Get DL name
$Dname=$DL.name
# Start outputting
write-output "`q$Dname`q" -nonewline
# Iterate over member list, outputting
foreach ($Member in $Dmember) {
$MName=$Member.DisplayName
write-output ",`q$MName`q" -nonewline
}
# Add terminal line-feed
write-output "`n"
}

Now the above will get me a display on screen but how do I engineer the above script to export to a text or xls file?
 
Associate
Joined
28 Oct 2003
Posts
816
At a quick glance, two quick options:

- run command with a pipe to out-file cmdlet, i.e. cmdlet.ps1 | out-file c:/scripts/output.txt. You can also do this from within the script.
- redirect output to a file i.e. cmdlet.ps1 > c:/scripts/output.txt

The out-file cmdlet gives you more options in terms of parameters for encoding etc.
 
Soldato
Joined
25 Mar 2004
Posts
15,903
Location
Fareham
Here try this, one line per member, with a column for which DL they are a member of. I think works better as a presentation method.

Code:
#Get Distribution Groups
$DLList = Get-DistributionGroup -ResultSize "unlimited"
#Array to hold members
$DLMembers = @()

#Loop through DL's
foreach ($DL in $DLList)
{
	#Get Members
	[array]$Members = Get-DistributionGroupMember $DL | Select RecipientType, PrimarySmtpAddress, @{N="DL";E={$DL.PrimarySmtpAddress}}
	
	#Append Current DL to $DLMembers Array
	if ($Members.Count -gt 0)
	{ $DLMembers += $Members }
}

#Export to CSV
$DLMembers | Export-Csv -NoType "C:\Temp\DLMembers.csv"
 
Last edited:
Back
Top Bottom