0

I have the following code:

$RecipientType = Get-Recipient $Name | Select-Object -Property RecipientType

if ($RecipientType.Equals("UserMailbox")) {
    Write-Host "Mailbox is OnPrem"          
}

I want to compare RecipientType value with string "UserMailbox", but it's not working...

5
  • Is there any particular reason you're using .Equals(), and not -eq (or -ceq is you need case sensitive comparison)? Commented Nov 17, 2020 at 12:12
  • -eq will also work. we can use both -eq and .Equals(). -ceq is used in case of case sensitivity. Commented Nov 17, 2020 at 12:15
  • I know that, but I'm asking whether there's a particular reason in this specific scenario Commented Nov 17, 2020 at 12:16
  • No nothing specific. Main focus is to use Select -ExpandProperty instead of Select-Object -Property in this case Commented Nov 17, 2020 at 12:25
  • Select and Select-Object is the same cmdlet (Select is just an alias) Commented Nov 17, 2020 at 13:15

2 Answers 2

1

For simplicity I'd use this:

if ((Get-Recipient $identity).RecipientType -eq 'usermailbox') {
  Write-Host 'Mailbox is OnPrem'
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here instead of using Select-Object -Property, use Select-Object -ExpandProperty because Select-Object returns an object. It can be done as below:

$RecipientType = (Get-Recipient $Identity | Select-Object -ExpandProperty RecipientType)

if ($RecipientType.Equals("UserMailbox")) {
    Write-Host "Mailbox is OnPrem"          
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.