Solved

Exporting Exchange contacts to CSV

Posted on 2014-10-07
8
206 Views
Last Modified: 2014-11-12
Hello Experts,

I am working though an issue where a user would like specific fields for all distribution group contacts exported to a CSV.

I have found a script on TechNet (http://gallery.technet.microsoft.com/office/Export-all-distribution-707c27eb) that does about half of the fields. I was able to update the script to include several new fields but seem to be hung on two of them. Can anyone assist me with getting the street address and mobile phone number to map correctly on the CSV. Currently the columns for these two fields show up blank.

Here is the script.
<#

.Requires -version 2 - Runs in Exchange Management Shell

.SYNOPSIS
.\DistributionGroupMemberReport.ps1 - It Can Display all the Distribution Group and its members on a List

Or It can Export to a CSV file


Example 1

[PS] C:\DG>.\DistributionGroupMemberReport.ps1


Distribution Group Member Report
----------------------------

1.Display in Shell

2.Export to CSV File

Choose The Task: 1

DisplayName                   Alias                         Primary SMTP streetAddress          Distriubtion Group
-----------                   -----                         --------------------          ------------------
Atlast1                       Atlast1                       Atlast1@targetexchange.in     Test1
Atlast2                       Atlast2                       Atlast2@careexchange.in       Test1
Blink                         Blink                         Blink@targetexchange.in       Test1
blink1                        blink1                        blink1@targetexchange.in      Test1
User2                         User2                         User2@careexchange.in         Test11
User3                         User3                         User3@careexchange.in         Test11
User4                         User4                         User4@careexchange.in         Test11
WithClient                    WithClient                    WithClient@careexchange.in    Test11
Blink                         Blink                         Blink@targetexchange.in       Test11
blink1                        blink1                        blink1@targetexchange.in      Test11

Example 2

[PS] C:\DG>.\DistributionGroupMemberReport.ps1


Distribution Group Member Report
----------------------------

1.Display in Shell

2.Export to CSV File

Choose The Task: 2
Enter the Path of CSV file (Eg. C:\DG.csv): C:\DGmembers.csv

.Author
Written By: Satheshwaran Manoharan

Change Log
V1.0, 11/10/2012 - Initial version

Change Log
V1.1, 02/07/2014 - Added "Enter the Distribution Group name with Wild Card"

Change Log
V1.2, 19/07/2014 - Added "Recipient OU,Distribution Group Primary SMTP streetAddress,Distribution Group Managers,Distribution Group OU"
V1.2.1, 19/07/2014 - Added "Option- Enter the Distribution Group name with Wild Card (Display)"
V1.2.2, 19/07/2014 - Added "Fixed "Hashtable-to-Object conversion is not supported in restricted language mode or a Data section"

V1.3,05/08/2014 - Hashtable-to-Object conversion is not supported - Fixed 
#>

Write-host "

Distribution Group Member Report
----------------------------

1.Display in Exchange Management Shell

2.Export to CSV File

3.Enter the Distribution Group name with Wild Card (Export)

4.Enter the Distribution Group name with Wild Card (Display)

Dynamic Distribution Group Member Report
----------------------------

5.Display in Exchange Management Shell

6.Export to CSV File

7.Enter the Dynamic Distribution Group name with Wild Card (Export)

8.Enter the Dynamic Group name with Wild Card (Display)"-ForeGround "Cyan"

#----------------
# Script
#----------------

Write-Host "               "

$number = Read-Host "Choose The Task"
$output = @()
switch ($number) 
{

1 {

$AllDG = Get-DistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}

2 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DG.csv)" 

$AllDG = Get-DistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType


$output += $UserObj  

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}



;Break}

3 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DG.csv)" 

$Dgname = Read-Host "Enter the DG name or Range (Eg. DGname , DG*,*DG)"

$AllDG = Get-DistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}

$userObj = New-Object PSObject


$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType

$output += $UserObj   

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}

;Break}

4 {

$Dgname = Read-Host "Enter the DG name or Range (Eg. DGname , DG*,*DG)"

$AllDG = Get-DistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}


5 {

$AllDG = Get-DynamicDistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}

6 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DYDG.csv)" 

$AllDG = Get-DynamicDistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType


$output += $UserObj  

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}

;Break}


7 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DYDG.csv)" 

$Dgname = Read-Host "Enter the DG name or Range (Eg. DynmicDGname , Dy*,*Dy)"

$AllDG = Get-DynamicDistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType

$output += $UserObj   

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}


;Break}

8 {

$Dgname = Read-Host "Enter the Dynamic DG name or Range (Eg. DynamicDGname , DG*,*DG)"

$AllDG = Get-DynamicDistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $members[$i].Mobile
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $members[$i].streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}

Default {Write-Host "No matches found , Enter Options 1 or 2" -ForeGround "red"}

}

Open in new window

0
Comment
Question by:PSC
  • 4
  • 4
8 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
Comment Utility
I have not gone through entire script but just updated the mobile phone and street address lines of code. You need to query AD properties to get these values. The script is trying to get them from Get-Distributiongroupmember. I guess there are several other values which are AD values.


<#

.Requires -version 2 - Runs in Exchange Management Shell

.SYNOPSIS
.\DistributionGroupMemberReport.ps1 - It Can Display all the Distribution Group and its members on a List

Or It can Export to a CSV file


Example 1

[PS] C:\DG>.\DistributionGroupMemberReport.ps1


Distribution Group Member Report
----------------------------

1.Display in Shell

2.Export to CSV File

Choose The Task: 1

DisplayName                   Alias                         Primary SMTP streetAddress          Distriubtion Group
-----------                   -----                         --------------------          ------------------
Atlast1                       Atlast1                       Atlast1@targetexchange.in     Test1
Atlast2                       Atlast2                       Atlast2@careexchange.in       Test1
Blink                         Blink                         Blink@targetexchange.in       Test1
blink1                        blink1                        blink1@targetexchange.in      Test1
User2                         User2                         User2@careexchange.in         Test11
User3                         User3                         User3@careexchange.in         Test11
User4                         User4                         User4@careexchange.in         Test11
WithClient                    WithClient                    WithClient@careexchange.in    Test11
Blink                         Blink                         Blink@targetexchange.in       Test11
blink1                        blink1                        blink1@targetexchange.in      Test11

Example 2

[PS] C:\DG>.\DistributionGroupMemberReport.ps1


Distribution Group Member Report
----------------------------

1.Display in Shell

2.Export to CSV File

Choose The Task: 2
Enter the Path of CSV file (Eg. C:\DG.csv): C:\DGmembers.csv

.Author
Written By: Satheshwaran Manoharan

Change Log
V1.0, 11/10/2012 - Initial version

Change Log
V1.1, 02/07/2014 - Added "Enter the Distribution Group name with Wild Card"

Change Log
V1.2, 19/07/2014 - Added "Recipient OU,Distribution Group Primary SMTP streetAddress,Distribution Group Managers,Distribution Group OU"
V1.2.1, 19/07/2014 - Added "Option- Enter the Distribution Group name with Wild Card (Display)"
V1.2.2, 19/07/2014 - Added "Fixed "Hashtable-to-Object conversion is not supported in restricted language mode or a Data section"

V1.3,05/08/2014 - Hashtable-to-Object conversion is not supported - Fixed 
#>

Write-host "

Distribution Group Member Report
----------------------------

1.Display in Exchange Management Shell

2.Export to CSV File

3.Enter the Distribution Group name with Wild Card (Export)

4.Enter the Distribution Group name with Wild Card (Display)

Dynamic Distribution Group Member Report
----------------------------

5.Display in Exchange Management Shell

6.Export to CSV File

7.Enter the Dynamic Distribution Group name with Wild Card (Export)

8.Enter the Dynamic Group name with Wild Card (Display)"-ForeGround "Cyan"

#----------------
# Script
#----------------

Write-Host "               "

$number = Read-Host "Choose The Task"
$output = @()
switch ($number) 
{

1 {

$AllDG = Get-DistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$ADProps = Get-User $Members[$i].Samaccountname

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}

2 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DG.csv)" 

$AllDG = Get-DistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
$ADProps = Get-User $Members[$i].Samaccountname

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType


$output += $UserObj  

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}



;Break}

3 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DG.csv)" 

$Dgname = Read-Host "Enter the DG name or Range (Eg. DGname , DG*,*DG)"

$AllDG = Get-DistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
$ADProps = Get-User $Members[$i].Samaccountname

$userObj = New-Object PSObject


$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.GroupType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType

$output += $UserObj   

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}

;Break}

4 {

$Dgname = Read-Host "Enter the DG name or Range (Eg. DGname , DG*,*DG)"

$AllDG = Get-DistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-DistributionGroupMember $Dg.name -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$ADProps = Get-User $Members[$i].Samaccountname
$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}


5 {

$AllDG = Get-DynamicDistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$ADProps = Get-User $Members[$i].Samaccountname
$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}

6 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DYDG.csv)" 

$AllDG = Get-DynamicDistributionGroup -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
$ADProps = Get-User $Members[$i].Samaccountname

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType


$output += $UserObj  

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}

;Break}


7 {

$CSVfile = Read-Host "Enter the Path of CSV file (Eg. C:\DYDG.csv)" 

$Dgname = Read-Host "Enter the DG name or Range (Eg. DynmicDGname , Dy*,*Dy)"

$AllDG = Get-DynamicDistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{

$managers = $Dg | Select @{Name='DistributionGroupManagers';Expression={[string]::join(";", ($_.Managedby))}}
$ADProps = Get-User $Members[$i].Samaccountname

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].Firstname
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $members[$i].RecipientType
$userObj | Add-Member NoteProperty -Name "Recipient OU" -Value $members[$i].OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Primary SMTP streetAddress" -Value $members[$i].PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name
$userObj | Add-Member NoteProperty -Name "Distribution Group Primary SMTP streetAddress" -Value $DG.PrimarySmtpstreetAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group Managers" -Value $managers.DistributionGroupManagers
$userObj | Add-Member NoteProperty -Name "Distribution Group OU" -Value $DG.OrganizationalUnit
$userObj | Add-Member NoteProperty -Name "Distribution Group Type" -Value $DG.RecipientType
$userObj | Add-Member NoteProperty -Name "Distribution Group Recipient Type" -Value $DG.RecipientType

$output += $UserObj   

}

$output | Export-csv -Path $CSVfile -NoTypeInformation

}


;Break}

8 {

$Dgname = Read-Host "Enter the Dynamic DG name or Range (Eg. DynamicDGname , DG*,*DG)"

$AllDG = Get-DynamicDistributionGroup $Dgname -resultsize unlimited

Foreach($dg in $allDg)

{

$Members = Get-Recipient -RecipientPreviewFilter $dg.RecipientFilter -resultsize unlimited

$Total = $Members.Count

$RemoveNull = $Total-1

For($i=0;$i -le $RemoveNull;$i++)

{
$ADProps = Get-User $Members[$i].Samaccountname

$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $members[$i].Name
$userObj | Add-Member NoteProperty -Name "Department" -Value $members[$i].Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $members[$i].Title
$userObj | Add-Member NoteProperty -Name "Phone" -Value $members[$i].Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $members[$i].city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $members[$i].StateOrProvince
$userObj | Add-Member NoteProperty -Name "postalcode" -Value $members[$i].postalcode
$userObj | Add-Member NoteProperty -Name "Location" -Value $members[$i].Location
$userObj | Add-Member NoteProperty -Name "First Name" -Value $members[$i].firstname
$userObj | Add-Member NoteProperty -Name "Last Name" -Value $members[$i].lastname
$userObj | Add-Member NoteProperty -Name "Alias" -Value $members[$i].Alias
$userObj | Add-Member NoteProperty -Name "Primary SMTP Address" -Value $members[$i].PrimarySmtpAddress
$userObj | Add-Member NoteProperty -Name "Distribution Group" -Value $DG.Name


Write-Output $Userobj

}

}

;Break}

Default {Write-Host "No matches found , Enter Options 1 or 2" -ForeGround "red"}

}

Open in new window

0
 

Author Comment

by:PSC
Comment Utility
Thanks Rajitha14! That fixed the mobile phone right away. The Addresses are all showing up the same. Is there an easier way to do this?

Here is the error while running the updated script. Note that we get quite a few of these messages. I suspect one for each contact.
Cannot process argument transformation on parameter 'Identity'. Cannot convert value "" to type
"Microsoft.Exchange.Configuration.Tasks.UserIdParameter". Error: "Parameter values of type
Microsoft.Exchange.Configuration.Tasks.UserIdParameter can't be empty. Specify a value, and try again.
Parameter name: identity"
    + CategoryInfo          : InvalidData: (:) [Get-User], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-User
    + PSComputerName        : server.domain.local

Cannot process argument transformation on parameter 'Identity'. Cannot convert value "" to type
"Microsoft.Exchange.Configuration.Tasks.UserIdParameter". Error: "Parameter values of type
Microsoft.Exchange.Configuration.Tasks.UserIdParameter can't be empty. Specify a value, and try again.
Parameter name: identity"
    + CategoryInfo          : InvalidData: (:) [Get-User], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-User
    + PSComputerName        : server.domain.local

Open in new window

0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
Comment Utility
The Addresses are all showing up the same. Is there an easier way to do this? Easier way of doing what? Is there any problem with addresses now?

It appears that few of your users dont have a samaccountname and hence you are getting the warnings. Try using Distinguished name instead of samaccountname. Replace the samaccountname with distinguishedname in all places where you find the below command.

$ADProps = Get-User $Members[$i].Samaccountname

$ADProps = Get-User $Members[$i].distinguishedname
0
 

Author Comment

by:PSC
Comment Utility
I made the change you suggested. The street address for the contacts are still not appearing in the exported csv. The result is below. In my last post I spoke to an "easier way" to do this. I was merely wondering if there was an easier way to obtain the end result of exporting contact information to a csv. Given that this spans across AD and Exchange I'm not sure that the script I'm using is the best tool for the job.

In a perfect word, the following fields would populate on the csv.
LastName, Firstname, email, Displayname, Department, Title, Phone, Mobile, StreetAddress, City, StateorProvince, Postalcode,recipientType,Distribution Group(s),

Sorry for the confusion.

Result:
The operation couldn't be performed because object 'domain.Local/OU/Internal/Contacts/Contact
Name' couldn't be found on 'DomainController'.
    + CategoryInfo          : NotSpecified: (:) [Get-User], ManagementObjectNotFoundException
    + FullyQualifiedErrorId : [Server=MailServer,RequestId=bf1e9b62-a127-42ce-b2dd-b538a6a25767,TimeStamp=10/8/2014 2
   :26:06 PM] B9CAEC29,Microsoft.Exchange.Management.RecipientTasks.GetUser
    + PSComputerName        : Mailserver.domain.local

Open in new window

I did confirm that the contacts DO exist in the Contacts OU in the error message above.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:Rajitha Chimmani
Comment Utility
Ok. I guess the script is doing more than what you need. the lines of code that I added work only for mailboxes and the command for contacts is different. Please let me know the following details


1. Exchange version
2. Do you have any Dynamic Distribution groups?
3. The list of fields you have given. Is that the only information that you are looking for?
0
 

Author Comment

by:PSC
Comment Utility
1. Exchange 2013
2. No
3. Yes
0
 
LVL 16

Accepted Solution

by:
Rajitha Chimmani earned 500 total points
Comment Utility
Please try the following

$ArrayObj = @()
$AllGroups = Get-DistributionGroup -Resultsize unlimited
foreach($group in $AllGroups){
$members = Get-DistributionGroupMember $group.Name
foreach($member in $members){
if($member.recipienttype -eq "MailContact"){$ADProps = Get-Contact $member.distinguishedname}
$ADProps = Get-User $member.Distinguishedname
$userObj = New-Object PSObject

$userObj | Add-Member NoteProperty -Name "LastName" -Value $ADProps.LastName
$userObj | Add-Member NoteProperty -Name "FirstName" -Value $ADProps.FirstName
$userObj | Add-Member NoteProperty -Name "DisplayName" -Value $member.DisplayName
$userObj | Add-Member NoteProperty -Name "Department" -Value $ADProps.Department
$userObj | Add-Member NoteProperty -Name "Title" -Value $ADProps.Title
$userObj | Add-Member NoteProperty -Name "Title" -Value $ADProps.Phone
$userObj | Add-Member NoteProperty -Name "Mobile" -Value $ADProps.MobilePhone
$userObj | Add-Member NoteProperty -Name "streetAddress" -Value $ADProps.streetAddress
$userObj | Add-Member NoteProperty -Name "city" -Value $ADProps.city
$userObj | Add-Member NoteProperty -Name "StateOrProvince" -Value $ADProps.StateOrProvince
$userObj | Add-Member NoteProperty -Name "PostalCode" -Value $ADProps.PostalCode
$userObj | Add-Member NoteProperty -Name "RecipientType" -Value $member.RecipientType
$userObj | Add-Member NoteProperty -Name "DistributionGroup" -Value $Group.DisplayName
}
$ArrayObj += $UserObj
}
$ArrayObj | Export-csv Groups.csv -NoTypeInformation

Open in new window

0
 

Author Comment

by:PSC
Comment Utility
Hi Rajitha14 - I'm away today but should be able to test tomorrow. Thank you for putting it together regardless.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now