0

Suppress PowerShell output messages new outlook application object is created?

$outlook = New-Object -ComObject outlook.application
$outlookItem = $outlook.CreateItem("olMailItem")

I already tried using these approaches. They did not work:

 $outlook = New-Object -ComObject outlook.application > $Null
 $outlook = New-Object -ComObject outlook.application |Out-Null
 ($outlook = New-Object -ComObject outlook.application) |Out-Null

These are the output message that I don't want them on the screen:

Application      : Microsoft.Office.Interop.Outlook.ApplicationClass
Class            : 5
...
...
Sherzad
  • 405
  • 4
  • 14
  • 4
    When I run `$outlook = New-Object -ComObject outlook.application`, I don't see any output. The object is created and assigned to `$outlook` as expected. – boxdog Apr 03 '19 at 10:27

1 Answers1

0

@boxdog, thanks for pointing it out. After many break points, I noticed the output is produced by Attachments.Add() method. It is resolved using >$null.

$outlookItem.Attachments.Add("fileName.txt") > $null
Sherzad
  • 405
  • 4
  • 14
  • 1
    Powershell-guru in their [Best Practice #12](http://powershell-guru.com/powershell-best-practice-12-avoid-out-null/): _it is recommended to **assign to `$null`** or **`[void]` casting** for better performance_. – JosefZ Apr 06 '19 at 10:10