Book Image

Microsoft Exchange Server Powershell Cookbook (Update)

Book Image

Microsoft Exchange Server Powershell Cookbook (Update)

Overview of this book

Table of Contents (21 chapters)
Microsoft Exchange Server PowerShell Cookbook Third Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Deleting e-mail items from a mailbox


The Exchange Management Shell provides cmdlets that allow you to delete items from one or more mailboxes. This can also be done with the EWS Managed API, and you can get a little more control over how the items are deleted compared to what the built-in cmdlets provide. In this recipe, you'll learn how to use the EWS Managed API to delete items from one or more mailboxes using PowerShell.

How to do it...

Let's see how to delete e-mail items from a mailbox using the following steps:

  1. First, load the assembly, create the ExchangeService object, and connect to EWS:

    Add-Type -Path C:\EWS\Microsoft.Exchange.WebServices.dll
    $svc = New-Object Microsoft.Exchange.WebServices.Data. ` ExchangeService
    $svc.AutoDiscoverUrl("[email protected]")
    
  2. Next, create a view of the total number of items that should be returned from the search:

    $view = New-Object -TypeName '
    Microsoft.Exchange.WebServices.Data.ItemView '
    -ArgumentList 100
    
  3. Create a property set that will include the...