Search code examples
powershelloutlookcom

Create MSOutlook Com-Object and make it visible


I would like to start MS Outlook for automation and make it visible. I'm typing command:

(new-object -com Outlook.Application).GetNamespace("MAPI").GetDefaultFolder("olFolderInbox").GetExplorer().Display()

Which throws error:

Exception setting "GetDefaultFolder": Cannot convert the "olFolderInbox" value of type "string" to type "Object".
At line:1 char:1
+ (new-object -com Outlook.Application).GetNamespace("MAPI").GetDefault ...

This doesn't work too:

$Outlook = New-Object -ComObject Outlook.Application 
$Outlook.visible = $True

Spoiler! This works:

(new-object -com Outlook.Application).GetNamespace("MAPI").GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).GetExplorer().Display()

Solution

  • The NameSpace.GetDefaultFolder method takes the type of default folder to return represented by the OlDefaultFolders enumeration (numeric values).