Exchange PS to get info with Variable

I am trying to pull some information for an O365 migration.

This command works fine:
get-mailbox -Filter {windowsemailaddress -eq "saorders@domain.com"} | fl windowsemailaddress, primarysmtpaddress

and returns the expected result.  I am going to use a CSV to make a variable for several email addresses.  For right now I hard coded a variable for simplicity:

$UPN = "saorders@domain.com"

If I plug that in:
get-mailbox -Filter {windowsemailaddress -eq "$UPN"} | fl windowsemailaddress, primarysmtpaddress

I get the following error:

Invoke-Command : Cannot bind parameter 'Filter' to the target. Exception setting "Filter": "The value "$upn" could not
be converted to type Microsoft.Exchange.Data.SmtpAddress."
At C:\Users\a_mscott\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\usp306.domain.com\usp306.domain.com.psm1:13613 char:
29
+             $scriptCmd = { & <<<<  $script:InvokeCommand `
    + CategoryInfo          : WriteError: (:) [Get-Mailbox], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.GetMailbox

What syntax do I need to use in this case to use a variable there?

March 31st, 2015 12:26pm

May be like this

$upn = "Chendrayan@Domain.com"

Get-Mailbox -Filter "WindowsEmailAddress -eq '$upn'"

I haven't tested but I assume it should do!

Free Windows Admin Tool Kit Click here and download it now
March 31st, 2015 1:01pm

Like a boss... thanks!

March 31st, 2015 2:35pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics