Yes, this is similar to the script in my previous post but is also different in the following ways:

This script searches the entire mailbox and looks for items in folders that contain mail items or in other words where the folders class is IPF.Note

This scripts also shows how to use the Not, Exists the AND logical operator to build up a Search Filter collection

I would suggest you read my previous post as it covers some fundamental used in this post.

To summarize what the script below does:

We start of by passing the SMTP address of the mailbox we want to search. We then do a deep folder traversal to get a list of all the folder where the FolderClass = “IPF.Note”.

We then pass to folder id to the StampPolicyOnItems function which then looks for Items which have a specific message class AND does not have the PR_POLICY_TAG property stamped on the item. This is done using Search Filter Collection.

Finally we loop through all the items that meet the criteria above and stamp the PR_POLICY_TAG and the PR_RETENTION_PERIOD properties on the item and update it.

Straight to code now. Create a .PS1 file with the code below:

# The script requires the EWS managed API, which can be downloaded here:# http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1# This also requires PowerShell 2.0# Make sure the Import-Module command below matches the DLL location of the API.# This path must match the install location of the EWS managed API. Change it if needed.

#Setup the View to get a limited number of Items at one time $folderView = new-object Microsoft.Exchange.WebServices.Data.FolderView($FpageSize,$FOffset,[Microsoft.Exchange.WebServices.Data.OffsetBasePoint]::Beginning) $folderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep $folderView.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet( [Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly, [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName, [Microsoft.Exchange.WebServices.Data.FolderSchema]::FolderClass);

while ($MoreItems) {#Setup the View to get a limited number of Items at one time $itemView = new-object Microsoft.Exchange.WebServices.Data.ItemView($pageSize,$Offset,[Microsoft.Exchange.WebServices.Data.OffsetBasePoint]::Beginning) $itemView.Traversal = [Microsoft.Exchange.WebServices.Data.ItemTraversal]::Shallow $itemView.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet( [Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly, [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ItemClass, [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Subject);

UserAccounts.txt contains the list of the users mailboxes in which the items needs to be stamped. This file should exist in the same directory as the .PS1 file.The First row denotes the Field Names. Format of the text file is as below: