Get-ContactsFromDomain.ps1


Description

Purpose

Retrieves MailContacts filtered by a specified domain.

Detailed Description

Get-ContactsFromDomain fetches MailContacts whose ExternalEmailAddress matches the provided domain. It constructs a search pattern based on the supplied domain and uses the Get-MailContact cmdlet to retrieve contacts. Selected properties are then output for review or further processing. This function supports ShouldProcess, allowing you to preview the operation with -WhatIf.

Back to Top

Usage

Example 1

Get-ContactsFromDomain -DomainName "gmail.com"

Retrieves all MailContacts with an ExternalEmailAddress that includes “@gmail.com”.

Back to Top

Notes

Author: Your Name Date: 2025-05-30

Back to Top


Script

<#
.SYNOPSIS
    Retrieves MailContacts filtered by a specified domain.

.DESCRIPTION
    Get-ContactsFromDomain fetches MailContacts whose ExternalEmailAddress matches the provided domain.
    It constructs a search pattern based on the supplied domain and uses the Get-MailContact cmdlet to retrieve
    contacts. Selected properties are then output for review or further processing. This function supports 
    ShouldProcess, allowing you to preview the operation with -WhatIf.

.PARAMETER DomainName
    Specifies the domain used to filter MailContacts. For example, "gmail.com" will retrieve contacts 
    whose ExternalEmailAddress contains "@gmail.com".

.EXAMPLE
    Get-ContactsFromDomain -DomainName "gmail.com"
    Retrieves all MailContacts with an ExternalEmailAddress that includes "@gmail.com".

.NOTES
    Author: Your Name
    Date: 2025-05-30
#>
function Get-ContactsFromDomain {
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
    param (
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = "Domain to filter MailContacts (e.g. gmail.com).")]
        [ValidateNotNullOrEmpty()]
        [string]$DomainName
    )

    begin {
        # Build the search pattern using the specified domain.
        $SearchPattern = "*@$DomainName*"
    }

    process {
        try {
            if ($PSCmdlet.ShouldProcess("MailContacts", "Retrieve contacts with ExternalEmailAddress matching '$SearchPattern'")) {
                $contacts = Get-MailContact -Filter "ExternalEmailAddress -like '$SearchPattern'" -ErrorAction Stop
                $contacts | Select-Object -Property Name, DisplayName, Alias, PrimarySMTPAddress, DistinguishedName
            }
        }
        catch {
            Write-Error "Failed to retrieve MailContacts: $_"
        }
    }
}

Back to Top

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.

Issue


Back to Top