Powershell Windows

PowerShell is so cool…

I had no idea how to automate Outlook with COM before today. Took me about 5 minutes to learn how with PowerShell. I knew it could be done with VBS, right? That’s how viruses are made. :)

This won’t be a long blog unfortunately, but wanted to paste real quick a series of commands which I basically figured out in a shell interactively. I didn’t have to read any docs, Get-Member told me all I needed to know.

I did go back and add some comments. I hope you find it useful.

$ol = New-Object -com Outlook.Application # starts outlook

$ol.Reminders | select caption, nextreminderdate # show reminders

$ol | gm -mem method # show all methods (actions) on the com object

$n = $ol.CreateItem('olNoteItem') # creates a note

$n # display note object

$n.Body = 'test' # set body of note

$n | gm # display object members (methods & properties) of note

$n.Display() # show note

$n.Color # display color value

$n.Color = 4 # set color

1..5 | ForEach-Object { $n.Color = $_; sleep 1 } # change color of note 5 times

$m = $ol.CreateItem('olMailItem') # create mail object

$m # display mail object

$m | gm -mem property # show mail properties

$m.Subject = 'test' # set subject

$m.Body = 'test' # set body

$m.To = 'email@domain.com' # set to address

$m.Display() # display mail item on screen 

$m.Send() # send mail item

$ol # display outlook object

$ol | gm # display outlook methods

: http://halr9000.com/article/836

2010-04-26 16:46:42

Hey Hal,

I cut and pasted and got the following error..

PS C:\Users\Mike> $n = $ol.CreateItem(‘olNoteItem’) # creates a note
Exception calling “CreateItem” with “1″ argument(s): “Type mismatch. (Exception from HRESULT: 0×80020005 (DISP_E_TYPEMI
SMATCH))”
At line:1 char:20
+ $n = $ol.CreateItem <<<< ('olNoteItem') # creates a note
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

Outlook 2007 on Win7 x64

  • Microblog

  • Recent Posts

  • Recent Comments

  • meta

  • PowerShell Blogroll