Mr-Goodbytes
Mr-Goodbytes

Reputation: 3

Simple PowerShell, Variable inside bracket no working

I'm missing formatting or something simple having to do with the variable. When I enter the variable data, no output. If I enter the SMTP: address manually script works fine.

$EmailAddressAlias=Read-Host "Enter the FULL Email Address to find the associated Mailbox "

Get-Mailbox -Identity * |
Where-Object {$_.EmailAddresses -like 'SMTP:$EmailAddressAlias'} |
Format-List Identity, EmailAddresses

Upvotes: 0

Views: 107

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36277

I see two issues here, and a suggestion. You've got a variable inside single quotes, and you have no wildcards in your -like comparison. In order for the variable to expand into its value you need to use double quotes like this:

$_.EmailAddresses -like "SMTP:$EmailAddressAlias"

Also, when you use -like with no wildcards you may as well be using -eq. Lastly, you should really filter at the Get-Mailbox level rather than getting all mailboxes, and then filtering for just the one you want. You may want to try this instead:

Get-Mailbox -Filter "EmailAddresses -like '*$EmailAddressAlias*'" | Format-List Identity, EmailAddresses

Upvotes: 1

Related Questions