How It Works
Module Check and Connection (Connect-ToExchangeOnline)
Verifies the ExchangeOnlineManagement module is present, installs it if not, then authenticates to Exchange Online. Returns $true on success or bails with an error, the main script won’t proceed if this fails.
Reading OOO Settings (Get-OutOfOfficeMessage)
Calls Get-MailboxAutoReplyConfiguration for the target mailbox and dumps the current state: enabled/disabled, internal message, external message, and scheduled start/end times if configured. Output is console-only, nothing is written to disk or modified.
Writing OOO Settings (Set-OutOfOfficeMessage)
Calls Set-MailboxAutoReplyConfiguration with the provided message, setting both internal and external reply bodies to the same value. Optionally accepts StartTime and EndTime to put the reply on a schedule (AutoReplyState becomes Scheduled under the hood when Exchange processes timed entries).
Interactive Menu Loop
Wraps the two functions in a do/while menu. Runs until the user picks Exit (option 3), which also cleanly disconnects the Exchange Online session via Disconnect-ExchangeOnline.
Usage
Prerequisites
- PowerShell 5.1+ or PowerShell 7+
- An account with at least the Mail Recipients or Organization Management role in Exchange Online (to read/write other users’ mailbox settings)
- Outbound internet access to connect to Exchange Online endpoints
Running the Script
.Set-OOOMessage.ps1Code language: PowerShell (powershell)
On launch it will:
1. Install ExchangeOnlineManagement if missing (requires an elevated session or user with rights to install modules)
2. Prompt for Exchange Online credentials via the standard Microsoft auth flow
3. Drop you into the interactive menu
Menu Options
1 - Get Out of Office Message # Read current OOO state for a mailbox
2 - Set Out of Office Message # Write a new OOO message, optionally scheduled
3 - Exit # Disconnect and quitCode language: Bash (bash)
Scheduling Format
When prompted for start/end times, use:
MM/DD/YYYY HH:MMExample: 07/01/2025 08:00
Caveats
- Internal and external messages are always set to the same value. If you need different messages for internal vs. external recipients, you’ll need to modify
Set-OutOfOfficeMessageto accept separate parameters and split them out before callingSet-MailboxAutoReplyConfiguration.
- There’s a dead
Clear-Hostafterreturn $trueinConnect-ToExchangeOnline. It never executes. Not harmful, just unreachable code.
- No HTML handling. Exchange stores OOO messages as HTML internally. Reading messages via the script may output raw HTML tags to the console. Setting plain text works, but recipients may see unformatted output depending on their client.
- Time zone dependency.
StartTimeandEndTimeare interpreted in the context of the mailbox’s configured time zone in Exchange, not necessarily the machine running the script. Verify results withGet-OutOfOfficeMessageafter setting a scheduled reply.
- Module auto-install runs silently with
-Force -AllowClobber. If you’re running this in a shared or managed environment, that may be undesirable. Consider pre-installing the module and removing the install block.
- No input validation on email addresses or date strings. A malformed date will throw an exception caught by the
catchblock and display an error, it won’t crash the script, but it also won’t tell you the format is wrong until after the fact.
- Session persistence. If the script is killed without reaching option 3, the Exchange Online session may linger until it times out on its own. Run
Disconnect-ExchangeOnline -Confirm:$falsemanually if that happens.
Full Script
# Script to Get and Set Out of Office Messages in Exchange Online
# Requirements: Exchange Online PowerShell V2 module must be installed
$host.ui.RawUI.WindowTitle = "Get and Set Out of Office Message"
$Host.UI.RawUI.BackgroundColor = 'Black'
Clear-Host
function Connect-ToExchangeOnline {
try {
# Check if ExchangeOnlineManagement module is installed
if (!(Get-Module -ListAvailable -Name ExchangeOnlineManagement)) {
Write-Host "Exchange Online Management module is not installed. Installing now..."
Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber
}
# Import the module
Import-Module ExchangeOnlineManagement
# Connect to Exchange Online
Connect-ExchangeOnline -ShowProgress $true
Write-Host "Successfully connected to Exchange Online" -ForegroundColor Green
return $true
Clear-host
}
catch {
Write-Host "Error connecting to Exchange Online: $($_.Exception.Message)" -ForegroundColor Red
return $false
}
}
function Get-OutOfOfficeMessage {
param (
[Parameter(Mandatory=$true)]
[string]$EmailAddress
)
try {
# Get the mailbox automatic replies settings
$autoReplySettings = Get-MailboxAutoReplyConfiguration -Identity $EmailAddress
Write-Host "`nOut of Office Settings for $EmailAddress :" -ForegroundColor Cyan
Write-Host "--------------------------------"
Write-Host "AutoReplyState: $($autoReplySettings.AutoReplyState)"
if ($autoReplySettings.AutoReplyState -ne "Disabled") {
Write-Host "`nInternal Message:"
Write-Host $autoReplySettings.InternalMessage
Write-Host "`nExternal Message:"
Write-Host $autoReplySettings.ExternalMessage
if ($autoReplySettings.StartTime) {
Write-Host "`nStart Time: $($autoReplySettings.StartTime)"
Write-Host "End Time: $($autoReplySettings.EndTime)"
}
}
}
catch {
Write-Host "Error getting Out of Office message: $($_.Exception.Message)" -ForegroundColor Red
}
}
function Set-OutOfOfficeMessage {
param (
[Parameter(Mandatory=$true)]
[string]$EmailAddress,
[Parameter(Mandatory=$true)]
[string]$Message,
[Parameter(Mandatory=$false)]
[DateTime]$StartTime,
[Parameter(Mandatory=$false)]
[DateTime]$EndTime
)
try {
$params = @{
Identity = $EmailAddress
AutoReplyState = "Enabled"
InternalMessage = $Message
ExternalMessage = $Message
}
# Add scheduled time if provided
if ($StartTime -and $EndTime) {
$params.Add("StartTime", $StartTime)
$params.Add("EndTime", $EndTime)
}
# Set the out of office message
Set-MailboxAutoReplyConfiguration @params
Write-Host "Out of Office message successfully set for $EmailAddress" -ForegroundColor Green
}
catch {
Write-Host "Error setting Out of Office message: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Connect to Exchange Online
if (!(Connect-ToExchangeOnline)) {
exit
}
do {
Write-Host "`nSelect an option:"
Write-Host "1. Get Out of Office Message"
Write-Host "2. Set Out of Office Message"
Write-Host "3. Exit"
$choice = Read-Host "`nEnter your choice (1-3)"
switch ($choice) {
"1" {
$email = Read-Host "`nEnter email address"
Get-OutOfOfficeMessage -EmailAddress $email
}
"2" {
$email = Read-Host "`nEnter email address"
$message = Read-Host "Enter Out of Office message"
$scheduled = Read-Host "Do you want to schedule the Out of Office message? (Y/N)"
if ($scheduled -eq "Y") {
$startTime = Read-Host "Enter start time (MM/DD/YYYY HH:MM)"
$endTime = Read-Host "Enter end time (MM/DD/YYYY HH:MM)"
Set-OutOfOfficeMessage -EmailAddress $email -Message $message -StartTime $startTime -EndTime $endTime
}
else {
Set-OutOfOfficeMessage -EmailAddress $email -Message $message
}
}
"3" {
Write-Host "`nDisconnecting from Exchange Online..."
Disconnect-ExchangeOnline -Confirm:$false
exit
}
default {
Write-Host "Invalid choice. Please try again." -ForegroundColor Yellow
}
}
} while ($true)Code language: PowerShell (powershell)