Skip to content

Set-ExchangeMailboxOutOfOfficeConfig

Exchange: Configures Automatic Replies (Out of Office) for a mailbox

#Requires -Version 5.1

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true)]
    [string]$Identity,

    [Parameter(Mandatory = $true)]
    [ValidateSet('Enabled', 'Disabled', 'Scheduled')]
    [string]$State,

    [string]$InternalMessage,

    [string]$ExternalMessage,

    [ValidateSet('None', 'Known', 'All')]
    [string]$ExternalAudience = 'All',

    [datetime]$StartTime,

    [datetime]$EndTime
)

Process {
    try {
        $params = @{
            'Identity'        = $Identity
            'AutoReplyState'  = $State
            'Confirm'         = $false
            'ErrorAction'     = 'Stop'
        }

        if ($State -ne 'Disabled') {
            if ($InternalMessage) { $params.Add('InternalMessage', $InternalMessage) }
            if ($ExternalMessage) { $params.Add('ExternalMessage', $ExternalMessage) }
            $params.Add('ExternalAudience', $ExternalAudience)

            if ($State -eq 'Scheduled') {
                if (-not $StartTime -or -not $EndTime) {
                    throw "StartTime and EndTime are required when State is 'Scheduled'."
                }
                $params.Add('StartTime', $StartTime)
                $params.Add('EndTime', $EndTime)
            }
        }

        Set-MailboxAutoReplyConfiguration @params

        $config = Get-MailboxAutoReplyConfiguration -Identity $Identity -ErrorAction Stop
        $result = [PSCustomObject]@{
            Identity       = $Identity
            AutoReplyState = $config.AutoReplyState
            StartTime      = $config.StartTime
            EndTime        = $config.EndTime
            Action         = "OOFConfigUpdated"
            Status         = "Success"
            Timestamp      = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

Specifies the Identity of the mailbox.

Specifies the state of the automatic replies (Enabled, Disabled, Scheduled).

Specifies the automatic reply message sent to internal senders.

Specifies the automatic reply message sent to external senders.

Specifies the audience for external automatic replies (None, Known, All).

Specifies the start time for scheduled automatic replies.

Specifies the end time for scheduled automatic replies.

An interactive directory of PowerShell scripts.