Skip to content

Commit

Permalink
Create Find-EmailAddress.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualox authored Mar 24, 2023
1 parent 5eb9d3d commit b2144ba
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Find-EmailAddress.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<#
.SYNOPSIS
Retrieves Active Directory objects with a specified email address or alias.
.DESCRIPTION
This script searches Active Directory for objects with a specified email address or alias, and outputs the object's username, display name, email address, and OU location.
.PARAMETER EmailAddress
The email address or alias to search for in Active Directory.
.EXAMPLE
.\Find-EmailAddress.ps1 -EmailAddress "[email protected]"
Searches Active Directory for objects with the email address or alias "[email protected]".
.INPUTS
String
.OUTPUTS
PSCustomObject
#>

[CmdletBinding()]
param (
[Parameter(Mandatory = $True,
HelpMessage = 'What e-mail address would you like to find?')]
[string]$EmailAddress
)

function Get-EmailAddress {
param (
[string]$Address
)

$adObjects = Get-ADObject -Properties mail, proxyAddresses, DisplayName -Filter "mail -like '*$Address*' -or proxyAddresses -like '*$Address*'"

foreach ($adObject in $adObjects) {
$ouPathComponents = ($adObject.DistinguishedName -split ",") | Where-Object { $_ -like "OU=*" }
$ou = ($ouPathComponents -join ",") -replace 'OU=', ''

[PSCustomObject]@{
UserName = $adObject.Name
DisplayName = $adObject.DisplayName
EmailAddress = $Address
OU = $ou
}
}
}


$results = Get-EmailAddress -Address $EmailAddress
$results | Select UserName, DisplayName, EmailAddress, OU | Format-Table -AutoSize

0 comments on commit b2144ba

Please sign in to comment.