Use PowerShell to add Holidays to Outlook Calendar

Geeking around with PowerShell today trying to add all company holidays to my Outlook calendar. In the script, I’m creating all day appointments and setting the Show As to out of office. Simple enough!

function get-mailfolders { 
	$outlookfolders = @() 
	$outlook = New-Object -ComObject Outlook.Application 
	foreach ($folder in $outlook.Session.Folders){ 

		foreach($mailfolder in $folder.Folders ) { 
			$olkf = New-Object PSObject -Property @{ 
				Path = $($mailfolder.FullFolderPath) 
				EntryID = $($mailfolder.EntryID) 
				StoreID = $($mailfolder.StoreID) 
			} 

			$outlookfolders += $olkf 
		} 
	} 
	$outlookfolders 
}

$outlook = new-object -com Outlook.Application
$folder = get-mailfolders | where {$_.Path -like "*calendar*" -and $_.Path -and $_.Path -like "*$mailbox*"} 
$calendar = $outlook.Session.GetFolderFromID($folder.EntryID, $folder.StoreID) 

$holidays = @{"01/01/2017"="New Year’s Day"; "01/16/2017"="Martin Luther King Day"; "02/02/2017"="Presidents Day"; "05/29/2017"="Memorial Day"; `
	"07/04/2017"="Independence Day"; "11/4/2017"="Labor Day"; "11/23/2017"="Thanksgiving Break"; "11/24/2017"="Thanksgiving Break"; "12/25/2017"="Christmas Day"}

foreach($holiday in $holidays.GetEnumerator() | Sort Key)
{
	[string]$hName = $holiday.Value
	$hDate = Get-Date $holiday.Key
	
	$appt = $calendar.Items.Add(1) 
	$appt.Start = $holiday.key.ToString()
	$appt.AllDayEvent = $true
	$appt.Subject = $hName
	$appt.Body = $hName

	<#
	Show As / Status
	0 = Free 
	1 = Tentative 
	2 = Busy 
	3 = Out of Office 
	#>
	$appt.BusyStatus = 3

	$appt.Save()
}

This post helped guide me in the right direction:
Create a calendar item

2 thoughts on “Use PowerShell to add Holidays to Outlook Calendar

  1. i am getting below error

    .\Holiday2018ps.txt.ps1 : File C:\Users\***********\Desktop\Holiday2018ps.txt.ps1 cannot be loaded because running
    scripts is disabled on this system. For more information, see about_Execution_Policies at
    https:/go.microsoft.com/fwlink/?LinkID=135170.
    At line:1 char:1
    + .\Holiday2018ps.txt.ps1
    + ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

  2. First, you might want to look at your file name. Then check your execution policy settings.

Leave a Reply

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