Exchange Set-OutOfOffice

Recently I’ve had the need to have a programmable solution to toggling the out-of-office auto-reply functionality on a users mailbox. The below script will let you disable or enable the feature or get the current status of the out-of-office state. In my scenario I was looking for a way to time of day based enabling/disabling of a standardized auto-reply feature for a service desk which is open during the day but not at night. When combining this script with a scheduled task we can easily accomplish this.

 

The script below was originally saved as: Set-OutOfOffice.ps1

 

if(Get-PSSnapin Microsoft.Exchange.Management.PowerShell.*){}
else {
    Remove-PSSnapin Add-PSSnapin Microsoft.Exchange.Management.PowerShell.*
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.*
}

$error = 0

if($args[0] -ne $NULL)
{
    $mailbox = $args[0]
    if($args[1] -ne $NULL)
    {
        $enabledisable = $args[1]
    }
    else
    {
        Write-host The script needs to be called in the format: .\Set-OutOfOffice.ps1 mailbox-to-adjust disable/enable/status
        $error = 1
    }
}
else
{
    Write-host The script needs to be called in the format: .\Set-OutOfOffice.ps1 mailbox-to-adjust disable/enable/status
    $error = 1
}



if($error -eq 0)
{
    if($enabledisable -like "disable")
    {
        Write-Host ""
        Write-Host ""
        write-host "Disabling auto-reply on mailbox: $mailbox"
        get-mailbox $mailbox | Set-MailboxAutoReplyConfiguration -AutoReplyState Disabled
        Write-Host ""
        Write-Host ""
    }
    if($enabledisable -like "enable")
    {
        Write-Host ""
        Write-Host ""
        write-host "Enabling auto-reply on mailbox: $mailbox"
        get-mailbox $mailbox | Set-MailboxAutoReplyConfiguration -AutoReplyState Enabled
        Write-Host ""
        Write-Host ""
    }
    if($enabledisable -like "status")
    {
        Write-Host ""
        Write-Host ""
        $mystate = get-mailbox $mailbox | Get-MailboxAutoReplyConfiguration | Select-Object AutoReplyState
        write-host "Auto-reply for mailbox" $mailbox "is currently configured as:" $mystate.AutoReplyState
        Write-Host ""
        Write-Host ""
    }
    
}

Leave a comment

Your email address will not be published. Required fields are marked *