Notice: Due to size constraints and loading performance considerations, scripts referenced in blog posts are not attached directly. To request access, please complete the following form: Script Request Form Note: A Google account is required to access the form.
Disclaimer: I do not accept responsibility for any issues arising from scripts being run without adequate understanding. It is the user's responsibility to review and assess any code before execution. More information

Powershell : Mailbox where art thou? - Checking for Valid Mailboxes


Managing Exchange environments often requires verifying whether users have mailboxes, especially during migrations, audits, or troubleshooting scenarios. In this post we will look at 2x PowerShell scripts that can streamline this process: one for comprehensive logging and another focused specifically on identifying users without mailboxes.

The Challenge

The script needed to check the following parameters:

  • Verify mailbox existence for a list of users
  • Distinguish between local and remote mailboxes (hybrid environments)
  • Generate reports for compliance or migration planning
  • Identify users who may need mailbox provisioning

Manual verification through the Exchange Management Console becomes impractical with large user lists. These scripts automate the process and provide clear, actionable results.

Script 1: Comprehensive Mailbox Verification Logger

The first script provides detailed logging of all mailbox verification attempts, whether successful or not.

# Ensure Exchange Management Shell is loaded before running this script

# Set paths
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$userListPath = Join-Path $scriptDir 'users.txt'
$logFilePath = Join-Path $scriptDir 'MailboxCheckLog.txt'

# Clear previous log
if (Test-Path $logFilePath) {
    Remove-Item $logFilePath -Force
}

# Header for log
"Mailbox Check Log - $(Get-Date)" | Out-File -FilePath $logFilePath
"==========================================" | Out-File -Append $logFilePath

# Read users from file
if (-Not (Test-Path $userListPath)) {
    Write-Error "File users.txt not found in $scriptDir"
    exit 1
}

$users = Get-Content $userListPath | Where-Object { $_ -and $_.Trim() -ne "" }

foreach ($user in $users) {
    $userTrimmed = $user.Trim()
    Write-Host "Checking user: $userTrimmed"

    try {
        $mailbox = Get-Mailbox -Identity $userTrimmed -ErrorAction Stop
        "$userTrimmed - Local Mailbox Found" | Out-File -Append $logFilePath
        continue
    } catch {
        # No local mailbox, continue to check remote
    }

    try {
        $remoteMailbox = Get-RemoteMailbox -Identity $userTrimmed -ErrorAction Stop
        "$userTrimmed - Remote Mailbox Found" | Out-File -Append $logFilePath
    } catch {
        "$userTrimmed - No Mailbox Found" | Out-File -Append $logFilePath
    }
}

Write-Host "Mailbox check completed. Log saved to: $logFilePath"

Script 2: Invalid Users CSV Reporter

The second script focuses specifically on users without mailboxes and exports results to a structured CSV format.

# Ensure Exchange Management Shell is loaded before running this script

# Set paths
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$userListPath = Join-Path $scriptDir 'users.txt'
$csvLogPath = Join-Path $scriptDir 'UsersWithoutMailbox.csv'

# Clear previous CSV log if it exists
if (Test-Path $csvLogPath) {
    Remove-Item $csvLogPath -Force
}

# Read users from file
if (-Not (Test-Path $userListPath)) {
    Write-Error "File users.txt not found in $scriptDir"
    exit 1
}

$users = Get-Content $userListPath | Where-Object { $_ -and $_.Trim() -ne "" }

# Prepare results array
$results = @()

foreach ($user in $users) {
    $userTrimmed = $user.Trim()
    Write-Host "Checking user: $userTrimmed"

    $hasMailbox = $false
    $mailboxType = "None"

    try {
        $null = Get-Mailbox -Identity $userTrimmed -ErrorAction Stop
        $hasMailbox = $true
        $mailboxType = "Local"
    } catch {}

    if (-not $hasMailbox) {
        try {
            $null = Get-RemoteMailbox -Identity $userTrimmed -ErrorAction Stop
            $hasMailbox = $true
            $mailboxType = "Remote"
        } catch {}
    }

    if (-not $hasMailbox) {
        $results += [PSCustomObject]@{
            Username     = $userTrimmed
            MailboxFound = $false
            MailboxType  = "None"
        }
    }
}

# Export to CSV
$results | Export-Csv -Path $csvLogPath -NoTypeInformation -Encoding UTF8

Write-Host "Done. CSV log of users without mailboxes saved to: $csvLogPath"

Setup and Usage

  1. Exchange Management Shell loaded on the machine running the scripts
  2. Appropriate permissions to query mailbox information
  3. users.txt file in the same directory as the script

Creating the User List:

Create a users.txt file with one username per line:

john.doe
jane.smith
mike.wilson
sarah.jones

Running the Scripts:

  1. Place the script file in your desired directory
  2. Create the users.txt file in the same directory
  3. Open Exchange Management Shell as administrator
  4. Navigate to the script directory
  5. Execute: .\mailbox-verification.ps1 or .\mailbox-verification-onlyinvalid.ps1

Conclusion

These scripts provide efficient, automated solutions for Exchange mailbox verification. Whether you need comprehensive logging or focused reporting on missing mailboxes, these tools can save significant time and reduce manual errors in your Exchange administration tasks.

Previous Post Next Post

نموذج الاتصال