Skip to content

New-Mailbox

Exchange Online: Creates a new mailbox and user account

#Requires -Version 5.1

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$Name,
    [Parameter(Mandatory = $true)]
    [string]$UserPrincipalName,
    [Parameter(Mandatory = $true)]
    [securestring]$Password,
    [string]$Alias,
    [string]$DisplayName,
    [string]$WindowsEmailAddress,
    [string]$FirstName,
    [string]$LastName,
    [string]$Office,
    [string]$Phone,
    [switch]$ResetPasswordOnNextLogon,
    [switch]$AccountDisabled
)

Process {
    try {
        [string[]]$Properties = @('AccountDisabled','Alias','DisplayName','Name','FirstName','LastName','Office','Phone','WindowsEmailAddress','ResetPasswordOnNextLogon','UserPrincipalName')

        $box = New-Mailbox -Name $Name -MicrosoftOnlineServicesID $UserPrincipalName -Password $Password -ResetPasswordOnNextLogon:$ResetPasswordOnNextLogon -Force -Confirm:$false -ErrorAction Stop

        if ($PSBoundParameters.ContainsKey('AccountDisabled')) {
            $null = Set-Mailbox -Identity $box.UserPrincipalName -AccountDisabled $true -Confirm:$false -ErrorAction Stop
        }
        if ($PSBoundParameters.ContainsKey('Alias')) {
            $null = Set-Mailbox -Identity $box.UserPrincipalName -Alias $Alias -Confirm:$false -ErrorAction Stop
        }
        if ($PSBoundParameters.ContainsKey('DisplayName')) {
            $null = Set-Mailbox -Identity $box.UserPrincipalName -DisplayName $DisplayName -Confirm:$false -ErrorAction Stop
        }
        if ($PSBoundParameters.ContainsKey('FirstName')) {
            $null = Set-User -Identity $box.UserPrincipalName -FirstName $FirstName -Confirm:$false -ErrorAction Stop
        }
        if ($PSBoundParameters.ContainsKey('LastName')) {
            $null = Set-User -Identity $box.UserPrincipalName -LastName $LastName -Confirm:$false -ErrorAction Stop
        }
        if ($PSBoundParameters.ContainsKey('Office')) {
            $null = Set-User -Identity $box.UserPrincipalName -Office $Office -Confirm:$false -ErrorAction Stop
        }
        if ($PSBoundParameters.ContainsKey('Phone')) {
            $null = Set-User -Identity $box.UserPrincipalName -Phone $Phone -Confirm:$false -ErrorAction Stop
        }
        if ($PSBoundParameters.ContainsKey('WindowsEmailAddress')) {
            $null = Set-Mailbox -Identity $box.UserPrincipalName -WindowsEmailAddress $WindowsEmailAddress -Confirm:$false -ErrorAction Stop
        }

        $result = Get-Mailbox -Identity $box.UserPrincipalName -ErrorAction Stop | Select-Object $Properties
        $result | ForEach-Object {
            $_ | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force
        }
    }
    catch { throw }
}

Unique name of the mailbox, maximum 64 characters

Logon name for the user account

Password for the mailbox

Alias name of the mailbox

Display name of the mailbox

Windows email address of the mailbox

User's first name

User's last name

User's physical office name or number

User's telephone number

Off

User is required to change their password the next time they log on

Off

Disable the account associated with the mailbox

An interactive directory of PowerShell scripts.