Download Attachment from outlook using Powershell

Viewed 290

I am using below code to download the attachment from email based on subject, but i am unbale to use like operater to check the subject and download, below is the code which i am using

I was tring for [Subject] like Test Powershell, but i am unable to achive this

**

$filepath = “D:\PowerShell”
 $filter="[Subject]=Test Powershell"
 $myNow = get-date -format "yyyy-MM-dd_hh-mm-ss"
 Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null 
 $olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]  
 $outlook = new-object -comobject outlook.application 
 $namespace = $outlook.GetNameSpace("MAPI") 
 $namespace.Logon("profilename","mypassword",$false,$false)
 $folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
#$folder.items|select *
$folder.items.Restrict($filter) |
  select -Expand Attachments | % {
    for ($i = $_.Count; $i; $i--) {
      $_.Item($i).SaveAsFile("$filepath\$($_.Item($i).FileName)")
    }
  }

**

1 Answers

When matching string properties, you can use either a pair of single quotes ('), or a pair of double quotes ("), to delimit a string that is part of the filter.

In specifying a filter in a Jet or DASL query, if you use a pair of single quotes to delimit a string that is part of the filter, and the string contains another single quote or apostrophe, then add a single quote as an escape character before the single quote or apostrophe. Use a similar approach if you use a pair of double quotes to delimit a string. If the string contains a double quote, then add a double quote as an escape character before the double quote.

For example, in the DASL filter string that filters for the Subject property being equal to the words Test Powershell, the entire filter string is delimited by a pair of double quotes, and the embedded string Test Powershell is delimited by a pair of single quotes. There are three characters that you need to escape in this filter string: the starting double quote and the ending double quote for the property reference of https://schemas.microsoft.com/mapi/proptag/0x0037001f, and the apostrophe in the value condition for the words Test Powershell.

$filter = "@SQL=""https://schemas.microsoft.com/mapi/proptag/0x0037001f"" = 'Test Powershell'"

or

$filter = "@SQL=""https://schemas.microsoft.com/mapi/proptag/0x0037001f"" like '%Test Powershell%'"
Related