Search notes:

Powershell module: Outlook

Functions

send-outlookMail Send an email with Outlook from PowerShell.
close-outlookWindows Closes (or at least tries to close) outlook items and to dismiss active reminders.
disable-outlookNotifications Do not show the distracting notificaction when an email message arrives.

Source code

Outlook.psm1

set-strictMode -version latest

function send-outlookMail {

   param(
      [parameter(mandatory = $true)]
      [string]    $recipient
   ,
      [parameter(mandatory = $true)]
      [string]    $subject
   ,
      [parameter(mandatory = $true)]
      [string]    $body
   ,
      [parameter(mandatory = $false)]
      [string[]]  $attachments
   )

#   $ol   = get-activeObject outlook.application
    $ol   = get-msOfficeComObject  outlook

    $email = $ol.createItem(0)  # 0 = olMailItem
    $email.to      = $recipient
    $email.subject = $subject
    $email.body    = $body

    foreach ($attachment in $attachments) {
    $resolved_path = resolve-path $attachment
       if (! (test-path $resolved_path)) {
          write-host "Attachment $resolved_path was not found"
          return
       }
     #
     # For some reason, apparently, resolved path
     # must be put into double quotes because otherwise,
     # the error
     #     Value does not fall within the expected range.
     # is thrown (which, imho, does not make lot of sense).
     #
       $null = $email.attachments.add("$resolved_path")
    }
 #  $email.display()
    $email.send()
}

function close-outlookWindows {
    $ol = get-msOfficeComObject outlook

  # 2021-11-29 / V.4: Loop multiple times
    $insLoopAgain  = $true
    while ($insLoopAgain) {
       write-host 'ins loop'

       $insLoopAgain = $false
       $ins_          = $ol.inspectors

       foreach ($ins in $ins_) {
          $insLoopAgain = $true 
          write-host "   $($ins.caption)"
          $ins.close(1) # 1 = olDiscard
       }
    }


#   2021-11-29 / V.4: Loop multiple times
    $rmdLoopAgain  = $true
    while ($rmdLoopAgain) {
       write-host 'rmd loop'

       $rmdLoopAgain = $false
       $rmd_          = $ol.reminders

       foreach ($rmd in $rmd_) {
          if ($rmd.isVisible) {
             $rmdLoopAgain = $true
             write-host "Reminder: $($rmd.caption)"
             $rmd.dismiss()
          }
          else {
          #  write-host "$($rmd.caption) is not visible"
          }
       }
    }
}

function disable-outlookNotifications {
   $regKeyOfficeRootV = get-msOfficeRegRoot

   $regKeyOfficeRootV

   set-itemProperty "$regKeyOfficeRootV\outlook\preferences" -name newMailDesktopAlerts -type dWord -value 0

}
Github repository ps-modules-Outlook, path: /Outlook.psm1

Outlook.psd1

@{
   RootModule        = 'Outlook.psm1'
   ModuleVersion     = '0.5'
   RequiredModules   = @(
      'MS-Office'
   )
   FunctionsToExport = @(
     'send-outlookMail',
     'close-outlookWindows'
     'disable-outlookNotifications'
    )
}
Github repository ps-modules-Outlook, path: /Outlook.psd1

History

V.2 Use get-msOfficeComObject to obtain an Outlook COM Object.
V.3 Add close-outlookWindows
V.4 close-outlookWindows: re-start iteration loop as long as inspectors and/or reminders were close (2021-11-29)
V.6 disable-outlookNotifications

TODO

2022-08-04: close-outlookWindows occasionally throws the error message the property 'isVisible' cannot be found on this object. (The offending code is if ($rmd.isVisible)).

See also

René's simple PowerShell modules

Index