Exchange Server 2016Exchange Server 2019

Exchange Server PowerShell Script | To Configure Virtual Directories

Quickly Set Internal and External URLs to Exchange Virtual Directory

Exchange Server PowerShell Script to Configure Virtual Directories

You can use Exchange Server PowerShell Script to configure virtual directories quickly.

After installing the Microsoft Exchange Server, the first step is to configure your virtual directories.

Microsoft Exchange Virtual directories have two types of URLs, Internal URLs, and External URLs

Therefore, in order to set the internal and external URLs for the virtual directory in the Exchange Server

You have to plan for the namespace according to your Exchange Server DNS settings

By default, these URLs are set to the Exchange server name and the local DNS

For Example, Our local domain DNS name is Techijack.local

So all the virtual directories will be set by default with this .local domain name.

However, if we want to connect to Exchange server from outside with OWA and Microsoft Outlook

We have to set the Public DNS name in External URLs so that users can connect from outside the organization.


Our Scenario for Techijack Exchange Server Organization

As our local domain in Techijack.local and our Public Domain is

The name of our Microsoft Exchange Server 2019 is EX01

So, currently, our Virtual directories URLs have been set as follows

ecp virtual directory

ServiceVirtual Directories URL
Exchange Control Panelhttps://EX01.techijack.local/ecp
Exchange Web Serviceshttps://EX01.techijack.local/EWS/Exchange.asmx
Exchange ActiveSynchttps://EX01.techijack.local/Microsoft-Server-ActiveSync
Offline Address Bookhttps://EX01.techijack.local/OAB
Outlook Web Apphttps://EX01.techijack.local/owa
MAPI over HTTPhttps://EX01.techijack.local/mapi
Outlook AnywhereEX01.techijack.local

So, by using the above Virtual directories URLs we can’t connect with Exchange Server from outside.

So, we have an External zone in our DNS by the name of our public domain i.e

 Exchange Server dns settings

Now we want all the virtual directories to set the public DNS name.

Our namespace will be mail. and we want all the External and Internal URLs to be the following

ServiceVirtual Directories URL
Exchange Control Panel
Exchange Web Services
Exchange ActiveSync
Offline Address Book
Outlook Web App


How To Create and Run Exchange Server PowerShell Script

Now we are planning to create and run the Exchange Server PowerShell Script to configure all the URLs in one go.

In order to configure the URL manually one by one, we have to use the following cmdlet for each service.

ECP : Get-ECPVirtualDirectory -Server EX01 | Set-ECPVirtualDirectory -InternalURL "" -ExternalURL ""
OWA : Get-OWAVirtualDirectory -Server $Server_name | Set-OWAVirtualDirectory -InternalURL "" -ExternalURL ""
OAB : Get-OABVirtualDirectory -Server $Server_name | Set-OABVirtualDirectory -InternalURL "" -ExternalURL ""
ActiveSync : Get-ActiveSyncVirtualDirectory -Server $Server_name | Set-ActiveSyncVirtualDirectory -InternalURL "" -ExternalURL ""
EWS : Get-WebServicesVirtualDirectory -Server $Server_name | Set-WebServicesVirtualDirectory -InternalURL "" -ExternalURL ""
Mapi : Get-MapiVirtualDirectory -Server $Server_name | Set-MapiVirtualDirectory -InternalURL "" -ExternalURL

However, if we create a script for this, every time we add a new server, we must run the script to configure the virtual directory.

Also, if you have multiple servers, same time you can run the script on all the servers.


Powershell Script To Configure Exchange Server Virtual Directories

With the help of the above cmdlet for each service, we will try to create a PowerShell Script.

So, First, we will set the variable for the Computer Name of the Exchange Server and NameSpace which we want to set.

Therefore, our variables are
$Server_Name = “EX01”

$FQDN = “”

Now in the place of the server name, we will put $Server_name and $FQDN at the place of the namespace

Our script will look like this below

$Server_name = "EX01"
$FQDN = ""
Get-OWAVirtualDirectory -Server $Server_name | Set-OWAVirtualDirectory -InternalURL "https://$($FQDN)/owa" -ExternalURL "https://$($FQDN)/owa"
Get-ECPVirtualDirectory -Server $Server_name | Set-ECPVirtualDirectory -InternalURL "https://$($FQDN)/ecp" -ExternalURL "https://$($FQDN)/ecp"
Get-OABVirtualDirectory -Server $Server_name | Set-OABVirtualDirectory -InternalURL "https://$($FQDN)/oab" -ExternalURL "https://$($FQDN)/oab"
Get-ActiveSyncVirtualDirectory -Server $Server_name | Set-ActiveSyncVirtualDirectory -InternalURL "https://$($FQDN)/Microsoft-Server-ActiveSync" -ExternalURL "https://$($FQDN)/Microsoft-Server-ActiveSync"
Get-WebServicesVirtualDirectory -Server $Server_name | Set-WebServicesVirtualDirectory -InternalURL "https://$($FQDN)/EWS/Exchange.asmx" -ExternalURL "https://$($FQDN)/EWS/Exchange.asmx"
Get-MapiVirtualDirectory -Server $Server_name | Set-MapiVirtualDirectory -InternalURL "https://$($FQDN)/mapi" -ExternalURL https://$($FQDN)/mapi
Set-ClientAccessService -Identity $Server_name -AutodiscoverServiceInternalURI https://$FQDN/Autodiscover/Autodiscover.xml
$OWA = Get-OWAVirtualDirectory -Server $Server_name -AdPropertiesOnly | Select InternalURL,ExternalURL
$ECP = Get-ECPVirtualDirectory -Server $Server_name -AdPropertiesOnly | Select InternalURL,ExternalURL
$OAB = Get-OABVirtualDirectory -Server $Server_name -AdPropertiesOnly | Select InternalURL,ExternalURL
$EAS = Get-ActiveSyncVirtualDirectory -Server $Server_name -AdPropertiesOnly | Select InternalURL,ExternalURL
$MAPI = Get-MAPIVirtualDirectory -Server $Server_name -AdPropertiesOnly | Select InternalURL,ExternalURL
$Auto_Discover = Get-ClientAccessServer $Server_name | Select AutoDiscoverServiceInternalUri

Once the above script will be executed, your virtual directories will be configured, along with Autodiscover.

Moreover, it will also display the result of external and internal URLs, which were set by the script.

Note: This script is valid for only one server, you have to modify the script to run for multiple servers.

Also, you have to change the computer name and FQDN name according to your scenario.

Furthermore, pls watch the video below to configure the Exchange server virtual Directory

Moreover, if you want to learn complete Exchange Server Administration you can register for the Exchange Server course

Back to top button