HOW TO:Using Exchange Web Services Managed API 1.0 from PowerShell 2.0

Requirement: Setting the OOF setting for a user using PowerShell Script.

The best and the easiest option I could think of was to use the Exchange Web Services Managed API 1.0 from with PowerShell 2.0. I finally got it to work!

 # The script requires the EWS managed API, which can be downloaded here:
# https://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.
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"# Create a new Exchange Service Object
$exchService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)# Set the Credentials
$exchService.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("Administrator","XYZ!","Mycompany")#Set the URL for the service
$exchService.Url= new-object Uri("https://localhost/EWS/Exchange.asmx")

$IoofReply =new-object Microsoft.Exchange.WebServices.Data.OofReply("I am OOF Internal")
$EoofReply =new-object Microsoft.Exchange.WebServices.Data.OofReply("I am OOF External")# Create a new OofSettings object
$OSettings = new-object Microsoft.Exchange.WebServices.Data.OofSettings# All,Known,None
$OSettings.ExternalAudience =[Microsoft.Exchange.WebServices.Data.OofExternalAudience]::All# Disabled,Enabled,Scheduled
$OSettings.State =[Microsoft.Exchange.WebServices.Data.OofState]::Enabled#Set The External and the Internal message
$OSettings.ExternalReply =$EoofReply
$OSettings.InternalReply =$IoofReply# The Duration parameter only need to be set if State is Scheduled
$startTime = [DateTime]::Today
$timeWindow = new-object Microsoft.Exchange.WebServices.Data.TimeWindow($startTime.AddDays(1), $startTime.AddDays(10))
$OSettings.Duration =$timeWindow#call the actual method with the SMTP address of the mailbox for which OOF is to be set
$exchService.SetUserOofSettings("akashb@mycompany.com",$OSettings)Write-Host "Done!"    

Enjoy!