PowerShell Tips – Outlook

In this article, we will use PowerShell to tinker around in Outlook (joking aside, until you are sure what the code you are planning to execute in Outlook actually does, use a test account or at least run the code line-by-line in debugging mode and check what happens. If your code runs out of control, it can cause a lot of confusion or even irreparable damage!).

First of all, we need to prepare the Outlook object and address space:

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")

Among the most common tasks is of course reading:

	$folder.Items | 
    	?{$_.subject -match "TEST" } |
    		sort receivedtime -desc | 
         		echo $_.body 

And sending messages:

$Mail = $Outlook.CreateItem(0)
$Mail.To = »ales@kompas-xnet.si«
$Mail.Subject = »TEST«
$Mail.Body =«testno sporočilo«

In the first case, we can notice a reference to DefaultFolder. Because the top folders in Outlook are enumerated, we can view them in this way:

$OutlookFolders = $Outlook.Session.Folders.Item(1).Folders
$OutlookFolders | ft FolderPath

Alternatively, we can reference them individually:

$OutlookDeletedITems = $Outlook.session.GetDefaultFolder(3)
$outlookOutbox = $Outlook.session.GetDefaultFolder(4)
$OutlookSentItems = $Outlook.session.GetDefaultFolder(5)
$OutlookInbox = $Outlook.session.GetDefaultFolder(6)
$OutlookCalendar = $Outlook.session.GetDefaultFolder(9)
$OutlookContacts = $Outlook.session.GetDefaultFolder(10)
$OutlookJournal = $Outlook.session.GetDefaultFolder(11)
$OutlookNotes = $Outlook.session.GetDefaultFolder(12)
$OutlookTasks = $Outlook.session.GetDefaultFolder(13)

If any of the folders above has subfolders, we can also reach them by name:


We can view messages in those folders in tabular mode in the following way:

$EmailsInFolder = $Outlook.Session.Folders.Item(1).Folders. Item(»Inbox«).Folders.Item(»Test«).Items
$EmailsInFolder | ft SentOn, Subject, SenderName, To, Sensitivity -AutoSize -Wrap

We can also reach the calendar:

$OutlookCalendar = $Outlook.session.GetDefaultFolder(9)

And read events from it:

$OutlookCalendar.Items | ft subject, start

Or create a new one:

$NewEvent = $Outlook.CreateItem(1)
$NewEvent.Subject = »Članek za piko«;
$NewEvent.Start = [datetime]«/4/26/2018«;

We can read tasks in this way:

$OutlookTasks = $Outlook.session.GetDefaultFolder(13).Items
$OutlookTasks | ft Subject, Body

Or, as in the case of the calendar, create new ones:

$newTaskObject =  $Outlook.CreateItem("olTaskItem")
$newTaskObject.Subject = »pika!«
$newTaskObject.Body = »nikar ne pozabi«

Finally, we may also be interested in contacts. We can list the in the following way:

$OutlookContacts = $Outlook.session.GetDefaultFolder(10).items
$OutlookContacts| Format-Table FullName,MobileTelephoneNumber,Email1Address

We can also add a new one:

$OutlookContacts = $Outlook.session.GetDefaultFolder(10)
$NewContact = $OutlookContacts.Items.Add()
$NewContact | gm
$NewContact.FullName = »Janez Novak«
$NewContact.Email1Address = »janez.novak@gmail.com«

Leave a Reply

Your e-mail address will not be published. Required fields are marked *