EN 
06.12.2025 Mikuláš WELCOME IN MY WORLD

This website is originally written in the Czech language. Most content is machine (AI) translated into English. The translation may not be exact and may contain errors.

Tento článek si můžete zobrazit v originální české verzi. You can view this article in the original Czech version.
Exchange Web Services a PowerShell

Exchange Web Services and PowerShell

| Petr Bouška - Samuraj |
For working with the Exchange server we have a number of cmdlets in PowerShell and for some GUI functions. But sometimes a slightly different way can be useful. We can use the Exchange Web Services Managed API, which is a .NET interface to EWS that uses the Web Services SOAP protocol and Autodiscover. And like other .NET layouts, we can use it straight from PowerShell.
displayed: 16 964x (15 779 CZ, 1 185 EN) | Comments [0]

Since Exchange 2007 SP1, the Exchange mail server includes Exchange Web Services (EWS), an interface for accessing items in the Exchange data store. For programming or scripting, the Exchange Web Services Managed API is available, which is a library that allows you to use EWS, even from PowerShell. This can be a useful tool, but the way to access user mailboxes seems peculiar. Even if you are an Exchange Admin, you do not have the permissions to retrieve information using EWS, and you must use Impersonation (described for Exchange 2007), which is, simply put, setting special permissions on user accounts.

This article is just a small guide on how to use EWS in PowerShell.

First, we need to load the EWS library.

Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"

We create an Exchange service object, and it's important to specify the Exchange version (otherwise, we'll get an error when binding, and specifying SP2 didn't work for me).

$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)

We must specify the CAS server address, either manually or automatically using Autodiscover (for a specific user).

$exchService.Url = "https://mail.company.local/EWS/Exchange.asmx"
$exchService.AutodiscoverUrl("bouska@company.cz")

Another important step is to determine which mailbox we'll access and under which permissions. We have three options. In the first two, we specify the user we'll access, and by default, we access their mailbox. The first option uses the current user's credentials.

$exchService.UseDefaultCredentials = $true

Or we can specify the credentials of a specific user, and we must also provide their password.

$exchService.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials($user, "password", "domain")

The last option is to use impersonation, where we access the account specified in this function. By default, this is under the user who ran the script, and they must have Impersonation rights.

$exchService.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, "someone@company.cz")

Now we can connect to a user's folder (folder). In the example, we'll connect to the calendar.

$calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar)

Here, we can, for example, display the permissions on the calendar. The second line displays information about the user assigned to the first item.

$calendar.Permissions
$calendar.Permissions[0].UserId

We can also set permissions (if permissions are already set for the given user, we'll get an error). At the end, we need to perform an update for the changes to take effect.

$FolderPermission = New-Object Microsoft.Exchange.WebServices.Data.FolderPermission("someone-else@company.cz", [Microsoft.Exchange.WebServices.Data.FolderPermissionLevel]::Reviewer)
$calendar.Permissions.Add($FolderPermission)
$calendar.Update()
Author:

Related articles:

Microsoft Exchange

Almost since the beginning of my practice, I have been involved in the administration of the Microsoft mail server, i.e. Exchange Server. I started with the 2003 version and worked my way up to Exchange Online. The articles cover many areas of management. Most since the migration to Exchange Server 2016 and its complete configuration. But also Exchange Hybrid and e-mail security.

PowerShell

Articles related to Microsoft's PowerShell scripting language, which is used in all new versions of MS OS and applications.

If you want write something about this article use comments.

Comments

There are no comments yet.

Add comment

Insert tag: strong em link

Help:
  • maximum length of comment is 2000 characters
  • HTML tags are not allowed (they will be removed), you can use only the special tags listed above the input field
  • new line (ENTER) ends paragraph and start new one
  • when you respond to a comment, put the original comment number in squar brackets at the beginning of the paragraph (line)