Menu Close

How to Setup a Recurring Out of Office (2023)

Are you looking for a PowerShell script to setup a recurring out of office after hours or specify a 16-hour task that begins at a specific time, such as 17:00 hours? You’re in the right place.

You need to automate replies to every email after the hours of 5 p.m. and 8 a.m. M-F. You would like to set the parameters so the task runs for 16 consecutive hours. What would be best is to have the auto-reply reply after 5 p.m. every day until 8 a.m. the next business day and also over the weekends. Are you struggling a bit with this?

Let’s get started.

Before we proceed, create two distribution lists, add the sub-DL as a member to the parent DL, and add the shared mailboxes and\or user mailboxes as members to the sub-DL.

Connect to Exchange Online

$O365Account = “admin email address”
$O365Password = ConvertTo-SecureString “admin password” -AsPlainText -Force
$LiveCred = New-Object System.Management.Automation.PSCredential ($O365Account, $O365Password)
Connect-Exchangeonline -Credential $LiveCred

Get members of the group

# Get the lists in the group
$dlmbrs=get-distributiongroupmember “APACOOF_Test”
foreach ($dlmbr in $dlmbrs) {

$g=Get-DistributionGroup $dlmbr.alias
$mbrs=Get-DistributionGroupMember $dlmbr.alias

Variables

$IsWorkday=0
$Workdays = $g.CustomAttribute4 -split “,”

Check if it is a workday

foreach ($Workday in $Workdays) {

if ( (get-date).ToString(“ddd”) -eq $Workday ) {
$IsWorkday=1

}
}

foreach ($mbr in $mbrs ) {

Workdays

The auto reply will start at 5 p.m. and end at 8 a.m. You can modify the start and end times as you wish.

if ($IsWorkday -eq 1) {

# Set start OOF hours
$d=(get-date).ToString(“dd/MMM/yyyy”) + ” ” + “05:00PM”
$st=get-date $d

# Set end OOF hours
$d=(get-date).AddDays(+1).ToString(“dd/MMM/yyyy”) + ” ” + “08:00AM”
$et=get-date $d

Set-MailboxAutoReplyConfiguration -Identity $mbr.PrimarySmtpAddress -AutoReplyState “Scheduled” -StartTime $st -EndTime $et

Weekends

} else { #off days

# Set start OOF hours
$d=(get-date).ToString(“dd/MMM/yyyy”) + ” ” + “09:00AM”
$st=get-date $d

# Set end OOF hours
$d=(get-date).AddDays(+1).ToString(“dd/MMM/yyyy”) + ” ” + “09:00AM”
$et=get-date $d

Set-MailboxAutoReplyConfiguration -Identity $mbr.PrimarySmtpAddress -AutoReplyState “Scheduled” -StartTime $st -EndTime $et

}

}

}

Full script to setup a recurring out of office daily

Open a new Notepad file. Paste the script below. Use the Ctrl+S keyboard shortcut to save it. This is where you will enter the name of the script and set its extension. Since we’re using Notepad, it will automatically add ‘*.txt” in the File name box.

You can change the extension by opening the ‘Save as type’ dropdown and selecting ‘All Files (.)’ from it. You can then enter the name of the file and its new extension in the File name field, e.g., Script.ps1.

###########################

# Connect to Exchange Online
$O365Account = “admin email address”
$O365Password = ConvertTo-SecureString “admin password” -AsPlainText -Force
$LiveCred = New-Object System.Management.Automation.PSCredential ($O365Account, $O365Password)
Connect-Exchangeonline -Credential $LiveCred

# Get the lists in the group
$dlmbrs=get-distributiongroupmember “Parent DL Name”
foreach ($dlmbr in $dlmbrs) {

# Get members of the group
$g=Get-DistributionGroup $dlmbr.alias
$mbrs=Get-DistributionGroupMember $dlmbr.alias

# Variables
$IsWorkday=0
$Workdays = $g.CustomAttribute4 -split “,”

# Check if it is a workday
foreach ($Workday in $Workdays) {

if ( (get-date).ToString(“ddd”) -eq $Workday ) {
$IsWorkday=1

}
}
foreach ($mbr in $mbrs ) {
if ($IsWorkday -eq 1) {

# Set start OOF hours
$d=(get-date).ToString(“dd/MMM/yyyy”) + ” ” + “05:00PM”
$st=get-date $d

# Set end OOF hours
$d=(get-date).AddDays(+1).ToString(“dd/MMM/yyyy”) + ” ” + “08:00AM”
$et=get-date $d

Set-MailboxAutoReplyConfiguration -Identity $mbr.PrimarySmtpAddress -AutoReplyState “Scheduled” -StartTime $st -EndTime $et

} else { #off days

# Set start OOF hours
$d=(get-date).ToString(“dd/MMM/yyyy”) + ” ” + “09:00AM”
$st=get-date $d

# Set end OOF hours
$d=(get-date).AddDays(+1).ToString(“dd/MMM/yyyy”) + ” ” + “09:00AM”
$et=get-date $d

Set-MailboxAutoReplyConfiguration -Identity $mbr.PrimarySmtpAddress -AutoReplyState “Scheduled” -StartTime $st -EndTime $et

}
}
}

###################################################

How do I run a PowerShell Script using the Task Scheduler?

The Microsoft Task Scheduler in Windows is used to schedule or launch an application or script at a specific time. It will only run a script when certain conditions are met. A script is a text file that consists of PowerShell commands. The “Task Scheduler” app can run repetitive tasks without putting in extra effort. It is helpful for system administrators as it reduces effort, saves time, and executes tasks at a specific time.

To schedule a PowerShell script in the “Task Scheduler” app, follow the given stepwise instructions.

  • Navigate to the “Startup” menu, search “Task Scheduler” and then open it.
recurring out of office
  • Click on “Create Task” on the right hand side.
  • Add the task name and an optional description.
  • Select “Run whether user is logged on or not.”
  • Select “Run with highest privileges.”
  • Move to the “Triggers” tab and click on the “New…” button to set the trigger.
  • Select frequency as “Daily.” Then, select the time and hit the “OK” button to finish setting the trigger.
  • Switch to the “Actions” tab and trigger the “New…” button.
  • Select “Start a program” from the “Action” section and click on the “Browse…” button to select the “PowerShell” program.
  • Navigate to this address: “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” or copy/paste it to the “Program/script” input section. When you find the “PowerShell” application, click on the “Open” button to launch it.
  • Now, add the script address in the “Add arguments” input section and hit the “OK” button to schedule the PowerShell script to run at the specific provided time. It will reset the Out of Office replies every day.

In this article, you learned how to setup a recurring out of office reply every day. Use the Out of Office reset PowerShell script and create a daily task in Windows Task Scheduler. From now on, every day, an automatic reply is sent to the sender. If the sender does not send an email, there will be no automatic reply.

Related Posts

Leave a Reply

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