title: How to delete mail items using office 365 REST API in provided powershell script sample
body: <p>I have been trying to find a solution to this problem for quite a while now and nothing I've found seems to work. I am hoping to use this script:</p>
  
  <pre><code>##-----------------------------------------------------## 
  ##        PICK AUTH Method                             ## 
  ##-----------------------------------------------------## 
  
  ## HARD CODING PSW    ## 
  #$password = ConvertTo-SecureString "xxx" -AsPlainText -Force 
  #$cred = New-Object System.Management.Automation.PSCredential "xxx@xxx.onmicrosofot.com",$password 
  
  ## USER PROMPT PSW    ## 
  #$cred = Get-Credential 
  
  ##-----------------------------------------------------## 
  ##    END PICK 
  ##-----------------------------------------------------## 
  
  $url = "https://outlook.office365.com/api/v1.0/me/messages" 
  $date = "2014-11-21" 
  
  ## Get all messages that have attachments where received date is greater than $date  
  $messageQuery = "" + $url + "?`$select=Id&amp;`$filter=HasAttachments eq true and DateTimeReceived ge " + $date 
  $messages = Invoke-RestMethod $messageQuery -Credential $cred 
  
  ## Loop through each results 
  foreach ($message in $messages.value) 
  { 
      # get attachments and save to file system 
      $query = $url + "/" + $message.Id + "/attachments" 
      $attachments = Invoke-RestMethod $query -Credential $cred 
  
      # in case of multiple attachments in email 
      foreach ($attachment in $attachments.value) 
      { 
          $attachment.Name 
          $path = "c:\Temp\" + $attachment.Name 
  
          $Content = [System.Convert]::FromBase64String($attachment.ContentBytes) 
          Set-Content -Path $path -Value $Content -Encoding Byte 
      } 
  } 
  </code></pre>
  
  <p>That I found from link - <a href="https://gallery.technet.microsoft.com/office/O365-Email-Attachments-to-6a45e84c#content" rel="nofollow">TechNet Office 365 Scripts</a></p>
  
  <p>I can get the script to work fine, it connects to mailbox and downloads the attachments for emails specified after the date variable. Problem is that I would like to run this on an hourly schedule and delete previously processed emails so that their attachments are not stripped on consecutive runs.</p>
  
  <p>I've tried lots of different commands to delete the email with no success and also tried altering the $messageQuery to include only items within a specific date range but can't get anything working.</p>
  
  <p>Can anyone provide the correct syntax for the delete command in powershell and an example of how to use it in this context please?</p>
  
  <p>Thanks</p>
  
  <p>Rich</p>
  
  <hr>
  
  <p>UPDATE - I think adding in:</p>
  
  <pre><code>$query2 = $url + "/" + $message.Id
  </code></pre>
  
  <p>And</p>
  
  <pre><code>Invoke-RestMethod $query2 -Credential $cred -Method Delete
  </code></pre>
  
  <p>Is getting me closer but I now get operation timed out.</p>
  
  <hr>
  
  <p>UPDATE - Full code as it stands and as requested:</p>
  
  <pre><code>##-----------------------------------------------------## 
  ##        PICK AUTH Method                             ## 
  ##-----------------------------------------------------## 
  
  ## HARD CODING PSW    ## 
  $password = ConvertTo-SecureString "password" -AsPlainText -Force 
  $cred = New-Object System.Management.Automation.PSCredential "email@address.co.uk",$password 
  
  ## USER PROMPT PSW    ## 
  ##$cred = Get-Credential 
  
  ##-----------------------------------------------------## 
  ##    END PICK 
  ##-----------------------------------------------------## 
  
  #$url = "https://outlook.office365.com/api/v1.0/me/messages" 
  $url = "https://outlook.office365.com/EWS/ODATA/me/messages"
  $today = Get-Date -format yyyy-MM-dd 
  $date = (Get-Date).AddDays(-2).ToString("yyyy-MM-dd")
  
  
  ## Set date and query  
  $messageQuery = "" + $url + "?`$select=Id&amp;`$filter=HasAttachments eq true and DateTimeReceived lt " + $today #+ " and DateTimeReceived gt" + $date
  $messages = Invoke-RestMethod $messageQuery -Credential $cred 
  
  ## Loop through each results 
  foreach ($message in $messages.value) 
  { 
      # get attachments and save to file system 
      $query = $url + "/" + $message.Id + "/attachments" 
      $query2 = $url + "/" + $message.Id
      $attachments = Invoke-RestMethod $query -Credential $cred 
  
      # in case of multiple attachments in email 
      foreach ($attachment in $attachments.value) 
      { 
          $attachment.Name 
          $path = "\\SomePath\" + $attachment.Name 
  
          $Content = [System.Convert]::FromBase64String($attachment.ContentBytes) 
          Set-Content -Path $path -Value $Content -Encoding Byte 
  
      } 
  
      Invoke-RestMethod $query2 -Credential $cred -Method Delete -TimeoutSec 100   
  
  } 
  
  #Move pdf files to another folder
  #Move-Item '\\SomePath\*.pdf' '\\SomePath'
  
  #Then delete any existing files from directory
  #Remove-Item '\\SomePath\*'
  </code></pre>
  
  <hr>
  
  <p>I tried to alter the date section to allow for a start and end date so the script could be run once a day, do the extraction but not reprocess attachments from previous days but ran into issues working out the syntax to alter the $messageQuery.</p>
  
  <p>Response from running the script  is:</p>
  
  <p>Invoke-RestMethod : The operation has timed out.
  At C:\Scripts\EmailExtractServiceContract365.ps1:26 char:13
  + $messages = Invoke-RestMethod $messageQuery -Credential $cred
  +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand</p>