From b2144bae93795846528223687b5359c630561d19 Mon Sep 17 00:00:00 2001 From: VirtualOx <37578273+virtualox@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:45:57 +0100 Subject: [PATCH] Create Find-EmailAddress.ps1 --- Find-EmailAddress.ps1 | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Find-EmailAddress.ps1 diff --git a/Find-EmailAddress.ps1 b/Find-EmailAddress.ps1 new file mode 100644 index 0000000..17581f1 --- /dev/null +++ b/Find-EmailAddress.ps1 @@ -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 "test@example.com" + Searches Active Directory for objects with the email address or alias "test@example.com". +.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