How do I find and match AD accounts with the existing script to include their emails from AD

My original post was solved (http://www.experts-exchange.com/Software/Server_Software/Active_Directory/Q_28668675.html#a40847974) but now i need to gather the email address from the AD user account when a perfect match comes up.  Any help please?
Joe GIT personalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NinjaStyle82Systems AdministratorCommented:
No idea if this works, but i think it will.
$UserFile = "C:\Temp\test.txt"
Import-Module ActiveDirectory
Get-Content -Path $UserFile | % {
	$Result = "" | Select-Object -Property FirstName, LastName, PerfectMatch, PossibleMatch
	$FirstName, $LastName = $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
	$Result.FirstName = $FirstName
	$Result.LastName = $LastName
	$SamId = $FirstName.SubString(0, 1) + $LastName
	If ($PossibleMatches = @(Get-ADUser -Filter "sAMAccountName -like '$($SamId)*'" -Properties mail)) {
		$Result.PossibleMatch = ($PossibleMatches | Select-Object -ExpandProperty SamAccountName) -join ", "
		If ($PossibleMatches.Count -eq 1) {
			$Result.PerfectMatch = $PossibleMatches.SamAccountName
                        $Result.Email = $PossibleMatches.mail
		} Else {
			If ($PerfectMatches = @($PossibleMatches | ? {($_.GivenName -eq $FirstName) -And ($_.Surname -eq $LastName)})) {
				If ($PerfectMatches.Count -eq 1) {
					$Result.PerfectMatch = $PerfectMatches.SamAccountName
                                        $Result.Email = $PossibleMatches.mail
				}
			}
		}
	}
	$Result | Write-Output
}

Open in new window

Joe GIT personalAuthor Commented:
didn't work.  see below

Exception setting "Email": "Property 'Email' cannot be found on this object; make sure it exists and is settable."
At D:\PSemailfindfile.ps1:18 char:41
+                                         $Result.Email = $PossibleMatches.mail
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting
NinjaStyle82Systems AdministratorCommented:
Oops, try this:
$UserFile = "C:\Temp\test.txt"
Import-Module ActiveDirectory
Get-Content -Path $UserFile | % {
	$Result = "" | Select-Object -Property FirstName, LastName, PerfectMatch, PossibleMatch, Email
	$FirstName, $LastName = $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
	$Result.FirstName = $FirstName
	$Result.LastName = $LastName
	$SamId = $FirstName.SubString(0, 1) + $LastName
	If ($PossibleMatches = @(Get-ADUser -Filter "sAMAccountName -like '$($SamId)*'" -Properties mail)) {
		$Result.PossibleMatch = ($PossibleMatches | Select-Object -ExpandProperty SamAccountName) -join ", "
		If ($PossibleMatches.Count -eq 1) {
			$Result.PerfectMatch = $PossibleMatches.SamAccountName
                        $Result.Email = $PossibleMatches.mail
		} Else {
			If ($PerfectMatches = @($PossibleMatches | ? {($_.GivenName -eq $FirstName) -And ($_.Surname -eq $LastName)})) {
				If ($PerfectMatches.Count -eq 1) {
					$Result.PerfectMatch = $PerfectMatches.SamAccountName
                                        $Result.Email = $PossibleMatches.mail
				}
			}
		}
	}
	$Result | Write-Output
}

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Joe GIT personalAuthor Commented:
almost there... when i do the export-csv function i get a "System.Object[]" for the emails.  Any way around this?
Joe GIT personalAuthor Commented:
however without the export-csv the onscreen output shows the emails but in a different format from the original script (it shows the rows instead of columns)
NinjaStyle82Systems AdministratorCommented:
That's weird, how are you passing the it to export-csv?
does the output on screen look correct?

Edit: I think it put it in rows just because we added a 5th attribute. added ft to the output should fix that
how about this
$UserFile = "C:\Temp\test.txt"
Import-Module ActiveDirectory
Get-Content -Path $UserFile | % {
	$Result = "" | Select-Object -Property FirstName, LastName, PerfectMatch, PossibleMatch, Email
	$FirstName, $LastName = $_.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
	$Result.FirstName = $FirstName
	$Result.LastName = $LastName
	$SamId = $FirstName.SubString(0, 1) + $LastName
	If ($PossibleMatches = @(Get-ADUser -Filter "sAMAccountName -like '$($SamId)*'" -Properties mail)) {
		$Result.PossibleMatch = ($PossibleMatches | Select-Object -ExpandProperty SamAccountName) -join ", "
		If ($PossibleMatches.Count -eq 1) {
			$Result.PerfectMatch = $PossibleMatches.SamAccountName
                        $Result.Email = $PossibleMatches.mail
		} Else {
			If ($PerfectMatches = @($PossibleMatches | ? {($_.GivenName -eq $FirstName) -And ($_.Surname -eq $LastName)})) {
				If ($PerfectMatches.Count -eq 1) {
					$Result.PerfectMatch = $PerfectMatches.SamAccountName
                                        $Result.Email = $PerfectMatches.mail
				}
			}
		}
	}
	$Result | Write-Output | ft
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joe GIT personalAuthor Commented:
that did it thank you!
NinjaStyle82Systems AdministratorCommented:
you're very welcome!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.