Saturday, April 21, 2018

Check If Mailbox Exist

Below simple code will check to see if mailbox exist, if not script will stop. It is easy to use same logic to setup similar conditions for similar tasks.

write-host "---------------------------------------" -f Yellow
$targetmb  = Read-Host "()_Please Provide user name"
write-host "---------------------------------------" -f Yellow

$sc = "SilentlyContinue"

write-host $null
Write-Host "(-)_.Checking mailbox:[$targetmb]" -f Yellow
$mb = get-mailbox  $targetmb -ErrorAction $sc

if ($mb -eq $null)
 write-host "`tThe target mailbox does NOT exist:" -n; Write-Host "[$targetmb ]" -f Red -b Black
write-host "`tLocated mailbox:" -n; Write-Host "[$targetmb]" -f Green -b Black

Casey DeDeal
Azure Certified Solutions Architect 
Principal Systems Engineer (blog)  (blog) (Twitter)

No comments:

Post a Comment

Connect-Azure-PowerShell if there is no existing Connection.

By Using MsolService to connect to Azure PowerShell, could be annoying time to time if unable to validate existing connection. The simpl...