Search code examples
powershellsuppressmessagenew-object

Suppress output created by PowerShell New-Object


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
...
...

Solution

  • @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