title: Powershell applied to Outlook Com Object - Multiple Filters
body: <p>ORIGINAL POST:</p>
  
  <p>Goal: an email is sent to a service account every day from a source.
  Take the csv file it gives me and put it in a folder for the DBA guy.</p>
  
  <p>Without further adieu, here is my current script:</p>
  
  <pre><code>#define variables
  $datestamp = (get-date).AddDays(-1).ToString("MMddyyyy")
  $datestamp = $datestamp.ToString()
  $path = "C:\MyPath"
  $dest = "C:\MyPath\Archive" 
  $file = "MyFile.csv"
  
  #create outlook session
  $objOutlook   = New-Object -Com Outlook.Application
  $inbox   = $objOutlook.Session.GetDefaultFolder(6)
  
  $inbox.Items.Restrict("[UnRead] = True" -and "[SenderEmailAddress] = 'SomePlace@SomeDomain.net'") | select -Expand Attachments | % 
  {
      for ($i = $_.Count; $i; $i--) 
      {
        $_.Item($i).SaveAsFile("C:\MyPath\$($_.Item($i).FileName)")
        $_.Parent.Unread = $false
      }
  } 
  
  if (Test-Path "C:\MyPath\*.csv")
  {
      if(((Get-ChildItem C:\MyPath | Measure-Object ).Count) -gt '1' )
          {
              Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy@domain.com" -To "PoorITGuy@domain.com" -Subject " FAIL" -Body "FAILED. Too many valid items from mailbox.
                  $objOutlook.quit()
              [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook) 
                  throw "Too many items to get."  
          }
      else
          {
           Get-ChildItem $path\*.csv | foreach {Copy-Item $_ "C:\MyPath\$"}
           Copy-Item C:\MyPath\*.csv  "$path\$file"
           Copy-Item C:\MyPath\*.csv "$dest\${datestamp}_$file"
  
              if(Test-Path "$dest\$file")
              {
                  Send-MailMessage –SmtpServer "server.domain.com" –From "PoorITGuy@domain.com"   -To "PoorITGuy@domain.com" -Subject "some message”
                  #cleanup - remove all files from base directory, clean mailbox, close out com object.
                  Remove-Item "$path\*.csv"
                  $inbox.Items | select | %
                  {
                      for ($i = $_.Count; $i; $i--) 
                      {
                       $_.Item($i).Delete
                      }
                          }
                  $objOutlook.quit()
                  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
              }
              else
              {
                  Send-MailMessage –SmtpServer "server.domain.com"  –From "PoorITGuy@domain.com" -To "PoorITGuy@domain.com" -Subject " failure" -Body "File manipulation failure."    
                  $objOutlook.quit()
                  [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
                      throw "File manipulation failure."
              }
  
          }
  }
  else
  {
          Send-MailMessage –SmtpServer "server.domain.com"  –From "PoorITGuy@domain.com" -To PoorITGuy@domain.com -Subject "FAIL" -Body "No item mailbox."
                  $objOutlook.quit()
                 [System.Runtime.Interopservices.Marshal]::ReleaseComObject($objOutlook)
                  throw "No item to get in inbox."  
  }
  </code></pre>
  
  <p>What doesn't work:</p>
  
  <pre><code>$inbox.Items.Restrict("[UnRead] = True" -and "[SenderEmailAddress] =  'SomePlace@SomeDomain.net'") | select -Expand Attachments | % 
  </code></pre>
  
  <p>It seems that you cannot restrict the comObject Outlook.Application with more than one filter.</p>
  
  <p>I've done a ton of searching on this and cannot find an answer on how to best perform this task in lieu of this. But I want my script to be spam-proof, so it needs to know that it is sent from the expected sender AND it needs to be unread only (see error catching right below)</p>
  
  <p>In addition, I'm not certain if this would work:</p>
  
  <pre><code>$inbox.Items | select | %
                      {
                          for ($i = $_.Count; $i; $i--) 
                          {
                           $_.Item($i).Delete
                          }
                              }
  </code></pre>
  
  <p>Also would like input on the script itself. Any input to make it more efficient would be appreciated.</p>