Select specific value from Multi-Value attribute

I need to be able to iterate through the values in a multi-value property (AD or Exchange) and set a variable with a specific value if it exists.

I have read numerous articles on how to add, remove specific values from/to a multi-value property but haven’t been able to successfully figure out how to capture a specific one into a variable for use later in the script.

Example: Set variable if the address @contoso.com exists

ProxyAddresses (AD) or EmailAddresses (Exchange) loop through these values and set $var1 to the value for the @contoso.com address if it exists.

smtp: Jane.Hancock@northwinds.com
smtp: Jane.Hancock@northwinds.net
smtp: Jane.Hancock@contoso.net
smtp: Jane.Hancock@contoso.com$

I have this much:

$var = get-mailbox jhancock | select EmailAddresses

If($var -like "*@contoso.com")
{

   What do I do here to capture the 'Jane.Hancock@contoso.com' into $var1?

}

It might not be the most efficient, but I’d do ForEach ($add in $Var) {IF($Add -like “*@contoso.com”) {$var1 = $Add}}.

 

At this point as long as it works, I will take it. I can revise for efficiency at a later date.

Thank you

 

$mailboxes = @()
$mailboxes += [pscustomobject]@{
    DisplayName = 'Jane Hancock'
    SamAccountName = 'jhancock'
    FirstName = 'Jane'
    LastName = 'Hancock'
    EmailAddresses = @(
        'smtp: Jane.Hancock@northwinds.com'
        'smtp: Jane.Hancock@northwinds.net',
        'smtp: Jane.Hancock@contoso.net',
        'smtp: Jane.Hancock@contoso.com$'
    )
}

$mailboxes += [pscustomobject]@{
    DisplayName = 'James Smith'
    SamAccountName = 'smith'
    FirstName = 'James'
    LastName = 'Smith'
    EmailAddresses = @(
        'smtp: James.Smith@northwinds.com'
        'smtp: James.Smith@northwinds.net',
        'smtp: James.Smith@contoso.net',
        'smtp: James.Smith@contoso.com$'
    )
}

$selectedUser = $mailboxes | Where {$_.SamAccountName -eq 'jhancock'}

$contosoEmail = $selectedUser.EmailAddresses | Where {$_ -like '*@contoso.com*'}

$contosoEmail