Link to home
Start Free TrialLog in
Avatar of Jeremy Weisinger
Jeremy Weisinger

asked on

How to check if user has an Exchange mailbox

I am writing a script that will perform some functions if the user has an Exchange mailbox. I'm getting stuck on how to check if the user has a mailbox.

Right now I'm using powershell but I can use VBS or batch commands if that's easier.

Here's what I have with powershell:
$strName = "testuser"

$strFilter = "(&(objectCategory=User)(samAccountName=$strName))"
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.Filter = $strFilter
$objPath = $objSearcher.FindOne()
$objUser = $objPath.GetDirectoryEntry()

$a = $objUser.msExchMailboxGuid

Open in new window




That works fine to get the mailbox GUID. What I would like to do is compare that $a variable to see if it's empty. But I can't figure out how to run a compare. Here's the value when there is no GUID:

PS C:\> Get-Variable -Name a |fl *

Name        : a
Description :
Value       : {}
Visibility  : Public
Module      :
ModuleName  :
Options     : None
Attributes  : {}

Open in new window


Does anyone have suggestions on how to do this with PowerShell or any other method?

Thanks!
Avatar of uescomp
uescomp
Flag of Afghanistan image

You could try:

Get-User user1 | Select-Object Name,RecipientType,RecipientTypeDetails
you do through query from the active directory users and computer, to show all user with a value in the email address field
SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jeremy Weisinger
Jeremy Weisinger

ASKER

@uescomp I should have given a little more detail. I'm running this command on an RDSH server. So no AD and no Exchange installed on the server so I can't run that command.

@waleeda Sorry but I need this automated in a script

@tgerbert Good suggestion but it's not working for the variable. I seemingly can't evaluate the value. When I run $a.value.length it just returns to the next prompt without giving output. Here's what I get:
PS C:\> $a.value.length
PS C:\>

Open in new window

It's almost like it has a null entry but when I try and compare it to null it just returns to the prompt. If I create the x variable with a null value I get the following:
PS C:\> $x = $null
PS C:\> $x.value.length
PS C:\>

Open in new window


But I can compare it... :
PS C:\> $x -eq $null
True
PS C:\>

Open in new window

but not the empty GUID:
PS C:\> $a -eq $null
PS C:\>

Open in new window

That above command should return either True or False but it is not returning anything. This is why I can't seem to compare it.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$a.Value -eq $null
Thanks for posting. You help me head in the right direction.