As system and infrastructure engineers, automating tasks is a fundamental aspect of our roles. Setting an out of office (OOO) reply on a Microsoft Exchange mailbox is a common task, especially when users are away from work. PowerShell provides a powerful way to accomplish this task programmatically, ensuring consistency and efficiency across your organisation.
Setting Out of Office with PowerShell
Prerequisites:
- Access to Microsoft Exchange server.
- Permissions to manage mailboxes.
- PowerShell
ExchangeOnlineManagement
module installed (Exchange Online) - PowerShell
ExchangeManagementShell
module installed (On-premise Exchange)
Here’s how you can Install the ExchangeOnlineManagement
& ExchangeManagementShell
modules:
This command should be run before attempting to use cmdlets like Connect-ExchangeOnline
, Set-MailboxAutoReplyConfiguration
, and Disconnect-ExchangeOnline
. Make sure you have the module installed on your system before running the script. You can install the module using the following command:
# Install Exchange Online Module Install-Module ExchangeOnlineManagement # Install Exchange on-premise Module Install-Module ExchangeManagementShell
Here’s how you can import the ExchangeOnlineManagement
& ExchangeManagementShell
modules:
# Import Exchange Online Module Import-Module ExchangeOnlineManagement # Import Exchange on-premise Module Import-Module ExchangeManagementShell
Once the required module is installed & imported, you can proceed with executing the script to manage out of office replies on Exchange mailboxes.
Connect to Exchange Server:
Before we can manage mailboxes, we need to establish a connection to the Exchange server. This can be achieved using the Connect-ExchangeOnline
cmdlet or New-PSSession
and Import-PSSession
for on-premises Exchange servers.
# For Exchange Online Connect-ExchangeOnline -UserPrincipalName <UPN> -ShowProgress $true # For on-premises Exchange $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://
Set Out of Office Message:
Once connected to the Exchange server, we can set the out of office message for a specific user. This is done using the Set-MailboxAutoReplyConfiguration
cmdlet. Below is an example code snippet to set the out of office message for a user named “John Doe” with a start and end date.
Set-MailboxAutoReplyConfiguration -Identity "John Doe" -AutoReplyState Enabled -InternalMessage "Out of office message for internal senders" -ExternalMessage "Out of office message for external senders" -StartTime "2024-04-25T08:00:00" -EndTime "2024-04-30T17:00:00"
In the above code:
-Identity
: Specifies the user for whom the out of office message is set.-AutoReplyState
: Enables the out of office reply.-InternalMessage
: Specifies the message to be sent to internal senders.-ExternalMessage
: Specifies the message to be sent to external senders.-StartTime
and-EndTime
: Define the period during which the out of office message will be active.
Disconnect from Exchange Server:
Once the task is complete, it’s good practice to disconnect from the Exchange server to release resources.
# For Exchange Online Disconnect-ExchangeOnline -Confirm:$false # For on-premises Exchange Remove-PSSession $Session
Full Code Examples
Here is a full PowerShell script for Microsoft Exchange Online with error handling utilising the code from above:
Import-Module ExchangeOnlineManagement # Connect to Exchange Online try { Connect-ExchangeOnline -UserPrincipalName <UPN> -ShowProgress $true -ErrorAction Stop } catch { Write-Error "Failed to connect to Exchange Online: $_" exit } # Set Out of Office Message try { Set-MailboxAutoReplyConfiguration -Identity "John Doe" -AutoReplyState Enabled -InternalMessage "Out of office message for internal senders" -ExternalMessage "Out of office message for external senders" -StartTime "2024-04-25T08:00:00" -EndTime "2024-04-30T17:00:00" -ErrorAction Stop } catch { Write-Error "Failed to set out of office message: $_" Disconnect-ExchangeOnline -Confirm:$false exit } # Disconnect from Exchange Online try { Disconnect-ExchangeOnline -Confirm:$false } catch { Write-Error "Failed to disconnect from Exchange Online: $_" exit }
Here is a full PowerShell script for Microsoft Exchange on-premise with error handling utilising the code from above:
Import-Module ExchangeManagementShell # Connect to Exchange Server try { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://your_exchange_server_uri/PowerShell/ -Authentication Kerberos Import-PSSession $Session -DisableNameChecking } catch { Write-Error "Failed to connect to Exchange Server: $_" exit } # Set Out of Office Message try { Set-MailboxAutoReplyConfiguration -Identity "John Doe" -AutoReplyState Enabled -InternalMessage "Out of office message for internal senders" -ExternalMessage "Out of office message for external senders" -StartTime "2024-04-25T08:00:00" -EndTime "2024-04-30T17:00:00" -ErrorAction Stop } catch { Write-Error "Failed to set out of office message: $_" Remove-PSSession $Session exit } # Disconnect from Exchange Server try { Remove-PSSession $Session } catch { Write-Error "Failed to disconnect from Exchange Server: $_" exit }
Conclusion
Utilising PowerShell for automating out of office replies on Microsoft Exchange mailboxes offers a streamlined approach for administrators. By integrating robust error handling into the script, system and infrastructure engineers can ensure reliable execution of mailbox management tasks. This automation not only saves time but also promotes consistency and accuracy in managing out of office messages across the organisation’s Exchange environment.