Menu Close

How to Setup a Recurring Out of Office (2024)

If you’re on the lookout for a PowerShell script to set up a recurring out of office response after regular working hours or schedule a 16-hour task starting at a specific time, like 5:00 PM, you’ve come to the right place!

Imagine being able to effortlessly automate email replies from 5:00 PM to 8:00 AM, Monday to Friday, and even over the weekends. Sounds like a plan, right? If you’re finding it a bit tricky, worry not—I’m here to help.

Let’s kick things off!

But first, let’s create two distribution lists, throw in the sub-DL as a member of the parent DL, and add those shared mailboxes or user mailboxes as members to the sub-DL in Exchange Online. Easy peasy!

Connect to Exchange Online:

For the lowdown on connecting to Exchange Online PowerShell, head over to the Microsoft website. They’ve got the deets you need!

$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:


Get ready for some customization magic! The auto-reply extravaganza begins at 5 p.m. and wraps up at 8 a.m. But hey, feel free to tweak those start and end times however you fancy! It’s your show.

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

}

}

}

Complete script to setup a recurring out of office daily:

Time to get hands-on! Pop open a fresh Notepad file, toss in the script below, and hit Ctrl+S to save the day. Notepad tends to be a bit extra, so it might try to sneak in a ‘*.txt’ extension. Foil its plans by selecting ‘All Files (.)’ from the ‘Save as type’ dropdown. Now, name your file—let’s say, Script.ps1—and hit that save button like a pro! You’re in control.

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

# 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 can I use Task Scheduler to run a PowerShell Script?

Time to unleash the power of Task Scheduler and get your PowerShell script on a routine! Follow these easy steps:

  • Hit up the “Startup” menu, type in “Task Scheduler,” and give it a click.
recurring out of office
  • Over on the right side, tap on “Create Task.”
recurring out of office outlook
  • Toss in a cool name and maybe a snazzy description.
  • Pick “Run whether user is logged on or not” because we’re all about efficiency.
  • Level up with “Run with highest privileges.”
set up recurring out of office outlook 365
  • Cruise over to the “Triggers” tab and hit up the “New…” button.
outlook recurring out of office
  • Opt for “Daily” in the frequency department. Set your chosen time and slam that “OK” button to seal the deal.
recurring automatic replies outlook
  • Swing by the “Actions” tab and hit the “New…” button.
  • Go for the “Start a program” action. Click “Browse.”
  • Navigate to “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe” (or paste it). Pop open that PowerShell magic by hitting “Open.”
  • Now, in the “Add arguments” section, throw in your script’s address. Hit “OK“.

Voila! Your PowerShell script is set to rock ‘n’ roll at the designated time, resetting those Out of Office replies every day.

There you have it! You’re now the maestro of automated daily replies. If no email comes a-knockin’, your script stays chill and quiet. Easy peasy!

Related Posts

Leave a Reply

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