Book Image

Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition - Second Edition

Book Image

Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition - Second Edition

Overview of this book

Microsoft Exchange Server 2013 is a complex messaging system. Windows PowerShell 3 can be used in conjunction with Exchange Server 2013 to automate and manage routine and complex tasks to save time, money, and eliminate errors.Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition offers more than 120 recipes and solutions to everyday problems and tasks encountered in the management and administration of Exchange Server. If you want to write scripts that help you create mailboxes, monitor server resources, and generate detailed reports, then this Cookbook is for you. This practical guide to Powershell and Exchange Server 2013 will help you automate and manage time-consuming and reoccurring tasks quickly and efficiently. Starting by going through key PowerShell concepts and the Exchange Management Shell, this book will get you automating tasks that used to take hours in no time.With practical recipes on the management of recipients and mailboxes as well as distribution groups and address lists, this book will save you countless hours on repetitive tasks. Diving deeper, you will then manage your mailbox database, client access, and your transport servers with simple but effective scripts.This book finishes with advanced recipes on Exchange Server problems such as server monitoring as well as maintaining high availability and security. If you want to control every aspect of Exchange Server 2013 and learn how to save time with PowerShell, then this cookbook is for you.
Table of Contents (23 chapters)
Microsoft Exchange Server 2013 PowerShell Cookbook
Credits
About the Authors
Acknowledgement
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...

  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 for 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 message ID. We then need to associate that property set with the $view...