Get-ADEmailAddress.ps1


Description

Purpose

Searches Active Directory for user accounts based on email address.

Detailed Description

The Get-ADEmailAddress function searches Active Directory for user accounts based on the provided email address. It supports wildcard matching and returns the object properties of matching accounts.

Back to Top

Usage

Example 1

Get-ADEmailAddress -EmailAddress john.doe@example.com

Searches Active Directory for user accounts with the email address “[email protected]” and returns their object properties.

Example 2

Get-ADEmailAddress -EmailAddress *@example.com

Searches Active Directory for user accounts with email addresses ending with “@example.com” and returns their object properties.

Back to Top

Notes

Author: Your Name Date: Today’s Date Version: 1.0

Back to Top


Script

<#
.SYNOPSIS
Searches Active Directory for user accounts based on email address.

.DESCRIPTION
The Get-ADEmailAddress function searches Active Directory for user accounts based on the provided email address. It supports wildcard matching and returns the object properties of matching accounts.

.PARAMETER EmailAddress
Specifies the email address to search for. Wildcards are supported.

.EXAMPLE
Get-ADEmailAddress -EmailAddress [email protected]
Searches Active Directory for user accounts with the email address "[email protected]" and returns their object properties.

.EXAMPLE
Get-ADEmailAddress -EmailAddress *@example.com
Searches Active Directory for user accounts with email addresses ending with "@example.com" and returns their object properties.

.INPUTS
None.

.OUTPUTS
System.Management.Automation.PSCustomObject

.NOTES
Author: Your Name
Date: Today's Date
Version: 1.0

.LINK
https://link-to-documentation

#>
function Get-ADEmailAddress {
    [CmdletBinding(
        SupportsShouldProcess = $true,
        ConfirmImpact = 'Medium'
    )]
    param (
        [Parameter(Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = 'Enter the AD object EmailAddress. This will return all accounts that match the entered value. Wildcards are supported.')]
        [SupportsWildcards()]
        [ValidateNotNullOrEmpty()]
        [string[]]$EmailAddress
    )
    BEGIN { }

    PROCESS {
        if ($PSCmdlet.ShouldProcess("$($EmailAddress)", "searching AD for user details.")) {
            try {
                Get-ADObject -Filter ' mail -like "$($EmailAddress)" ' -Properties * | Select-Object -Property DistinguishedName, ObjectClass, Name, mail
            }
            catch {
                Write-Error -Message "$_"
            }
        }
    }
}

# function to search all attributes of an AD User or Contact object for an email address and return the object properties if found

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