Get-MailContactDetails.ps1
19 Sep 2025Description
Purpose
Retrieves detailed information and distribution group memberships for a specified mail contact.
Detailed Description
The Get-MailContactDetails function takes a specific email address as input and retrieves detailed information about the corresponding mail contact, including their distribution group memberships. It validates the email format, fetches the mail contact and underlying contact object, and lists all distribution groups the contact is a member of.
Usage
Example 1
Get-MailContactDetails -ContactEmail "[email protected]"
Retrieves detailed information for the mail contact with the specified email address.
Example 2
Get-MailContactDetails -ContactEmail "[email protected]"
Returns an error if the specified email address is not found or is invalid.
Notes
Author: Your Name Date: 2024-06-30
Script
<#
.SYNOPSIS
Retrieves detailed information and distribution group memberships for a specified mail contact.
.DESCRIPTION
The Get-MailContactDetails function takes a specific email address as input and retrieves
detailed information about the corresponding mail contact, including their distribution group memberships.
It validates the email format, fetches the mail contact and underlying contact object, and lists all
distribution groups the contact is a member of.
.PARAMETER ContactEmail
The email address of the mail contact to retrieve details for. This parameter is mandatory.
.EXAMPLE
Get-MailContactDetails -ContactEmail "[email protected]"
Retrieves detailed information for the mail contact with the specified email address.
.EXAMPLE
Get-MailContactDetails -ContactEmail "[email protected]"
Returns an error if the specified email address is not found or is invalid.
.NOTES
Author: Your Name
Date: 2024-06-30
#>
function Get-MailContactDetails {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$ContactEmail
)
# Validate the email address format
if ($ContactEmail -notmatch '^[\w\.-]+@[\w\.-]+\.\w{2,}$') {
Write-Error "Invalid email address format: $ContactEmail"
return
}
# Get the mail contact
$mailContact = Get-MailContact -Identity $ContactEmail -ErrorAction SilentlyContinue
if ($null -eq $mailContact) {
Write-Error "Mail contact with email address $ContactEmail not found."
return
}
# Get the underlying contact object
$contact = Get-Contact -Identity $ContactEmail | Select-Object -Property *
# Get the distribution groups the contact is a member of
$groups = Get-DistributionGroup -ResultSize Unlimited | Where-Object {
(Get-DistributionGroupMember -Identity $_.Identity | Where-Object { $_.PrimarySmtpAddress -eq $ContactEmail })
}
$groupNames = $groups | Select-Object -ExpandProperty Name -Unique
# Create a custom object with the contact's detailed information and group memberships
$contactDetails = [PSCustomObject]@{
DisplayName = $mailContact.DisplayName
EmailAddress = $mailContact.PrimarySmtpAddress
Alias = $mailContact.Alias
FirstName = $contact.FirstName
LastName = $contact.LastName
ExternalEmailAddress = $mailContact.ExternalEmailAddress
OrganizationalUnit = $mailContact.OrganizationalUnit
DistinguishedName = $mailContact.DistinguishedName
Title = $contact.Title
Department = $contact.Department
Company = $contact.Company
StreetAddress = $contact.StreetAddress
City = $contact.City
StateOrProvince = $contact.StateOrProvince
PostalCode = $contact.PostalCode
CountryOrRegion = $contact.CountryOrRegion
Phone = $contact.Phone
Fax = $contact.Fax
HomePhone = $contact.HomePhone
MobilePhone = $contact.MobilePhone
Pager = $contact.Pager
Notes = $contact.Notes
Groups = $groupNames -join ";"
}
return $contactDetails
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.