Export and Import of an SSL/TLS Certificate in Exchange Server

When I add a new Exchange Server to the organisation, I need to install the SSL/TLS certificate that I use on the existing servers on the new server as well, so that clients can connect to it without a security warning. Exchange Setup installs its own self-signed certificate on every server. This certificate encrypts the traffic, but clients do not trust it automatically. For the client not to receive a warning, the certificate that the server presents must cover the DNS name the client connects to, must be within its validity period and must chain to a Certificate Authority (CA) that the client trusts. The existing certificate that meets these conditions must be moved to the new server together with its private key and assigned to the relevant services.

The move is done in PFX format. A PFX keeps the certificate and its private key in a single password-protected file. The password that I set during the export is requested again during the import, and the file cannot be used without the password. I confirm that the certificate is the same certificate on every server by its thumbprint. The thumbprint is the hash value generated from the content of the certificate, and the same certificate appears with the same thumbprint on every server.

Below, I export the certificate from the EXCHSRV01 server, import it to EXCHSRV02, the server that I have just added to the organisation, assign it to the IIS and SMTP services, and check that the assignment is reflected in the IIS bindings. I do all of these steps in the Exchange Management Shell with PowerShell commands.

The Export-ExchangeCertificate -BinaryEncoded and Import-ExchangeCertificate -FileData method in this article works in the same way on Exchange Server 2013, 2016, 2019 and SE. Because the -FileName parameter was removed as of Exchange Server 2016 CU23 and 2019 CU12, this is the only method in the Exchange Management Shell.

1- Preliminary Checks

Before I move on to the commands, I make sure that the conditions below are met. I verify the status and the validity date of the certificate from the output of the query in section 3, and the private key with the command in this section.

Check Expected State
Exchange permissions The account that performs the operation must be a member of the Organization Management or Server Management role group.
Windows permissions Access to the \\EXCHSRV01\C$\CERT path is required. For the Invoke-Command steps in sections 7 and 8, WinRM must be enabled on the target server and the account must be a local administrator. These permissions are separate from the Exchange permissions.
Certificate status The Status value must be Valid, and the NotAfter date must not be close.
Private key The HasPrivateKey and PrivateKeyExportable values of the certificate on the source server must be True.
Record of the current state The InternalTransportCertificateThumbprint value of the target server and the thumbprint on the Default Web Site bindings must be noted.

I check the private key on the source server EXCHSRV01 with the thumbprint that I note in section 3. If either of the 2 values is False, the certificate cannot be exported together with its private key.


Get-ExchangeCertificate -Thumbprint 74805915260BBD4C868543D8D84004639FFAE545 | Format-List HasPrivateKey, PrivateKeyExportable

On the target server, I get the thumbprint of the certificate used for SMTP traffic between servers with the command below and note it. I record the current thumbprint on the Default Web Site bindings by running the command in section 8 once before the change. These 2 values show which certificate I return to if I need to roll back.


Get-TransportService EXCHSRV02 | Format-List Name, InternalTransportCertificateThumbprint

2- Getting the Server Information

First, I get the summary information of the Exchange Servers in the organisation. The command below lists the name, the roles and the version of each server, and the site it belongs to.

EXCHSRV02 is the server that I have just added to the organisation and that does not have an SSL/TLS certificate assigned yet. In this output, I verify the role and the version of EXCHSRV02 and whether it is in the same site as EXCHSRV01.


Get-ExchangeServer | FL Name,ServerRole,AdminDisplayVersion,Site

Exporting and Importing an SSL Certificate

3- Querying the SSL/TLS Certificate Information

To find the certificate that I am going to move, I filter the certificates on the server. The command below lists the certificates whose status is Valid and that are not self-signed. The filter does not distinguish the type of CA that issued the certificate, and it can return more than 1 certificate. I identify the certificate to move by the domain names in the CertificateDomains field and by the thumbprint.

In the output, FriendlyName and Subject show the name of the certificate and who it belongs to, and CertificateDomains shows the domain names that it covers. The Services field shows which Exchange services the certificate is assigned to, and NotBefore and NotAfter give the validity period. The value that I use in all of the following steps is the Thumbprint value.


Get-ExchangeCertificate | Where-Object { $_.Status -eq "Valid" -and $_.IsSelfSigned -eq $false } | Format-List FriendlyName, Subject, CertificateDomains, Thumbprint, Services, NotBefore, NotAfter

I run this command on the EXCHSRV01 server, where the certificate is installed. For this reason, I did not use the -Server parameter. I copy the thumbprint 74805915260BBD4C868543D8D84004639FFAE545 from the output and note it.

Exporting and Importing an SSL Certificate

I now run the same query for the server where the certificate is not installed. Because I still run the command from EXCHSRV01, I add the -Server EXCHSRV02 parameter. The output does not contain a certificate with this thumbprint.


Get-ExchangeCertificate -Server EXCHSRV02 | Where-Object { $_.Status -eq "Valid" -and $_.IsSelfSigned -eq $false } | Format-List FriendlyName, Subject, CertificateDomains, Thumbprint, Services, NotBefore, NotAfter

Exporting and Importing an SSL Certificate

4- Exporting the SSL/TLS Certificate

Before the export, I prepare the folder that the file will be written to. I create a folder named CERT under the C:\ directory. C$ is an administrative share that Windows creates by default and that members of the local Administrators group can access. This allows me to access the folder over the network with the Universal Naming Convention (UNC) path \\EXCHSRV01\C$\CERT, without defining a separate share.

Exporting and Importing an SSL Certificate

I deliberately choose a short path with no spaces and no special characters. Spaces and special characters in a path can cause commands to fail, especially in scripts. I verify that I can access the folder through the UNC path.

Exporting and Importing an SSL Certificate

The Export-ExchangeCertificate command exports the certificate that I specify by thumbprint in PKCS #12 format. The -BinaryEncoded parameter returns the data in DER binary encoding. When it is used together with -Password, the certificate and the private key can be stored in a single binary file. The password protects the exported private key.

The command does not write the file to disk itself. The certificate data is held in the FileData property of the $cert variable, and [System.IO.File]::WriteAllBytes in the second line writes these bytes to the path that I specify as a .pfx file.

In the example below, the password is in the command as plain text. In a production environment, it is better to pass (Read-Host "Enter password" -AsSecureString) to the -Password parameter and type the password at the prompt, so that the password does not remain in the command history.


$cert = Export-ExchangeCertificate -Thumbprint '74805915260BBD4C868543D8D84004639FFAE545' -BinaryEncoded -Password (ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force)
[System.IO.File]::WriteAllBytes('\\EXCHSRV01\C$\CERT\Wildcard_firatboyan.com.pfx', $cert.FileData)

Exporting and Importing an SSL Certificate

Checkpoint
The command must complete without an error, and the Wildcard_firatboyan.com.pfx file must exist in the \\EXCHSRV01\C$\CERT folder.

The PFX file contains the private key of the certificate. Do not leave the file in this folder after the import is finished. Delete it or move it to a location with restricted access.

5- Importing the SSL/TLS Certificate

The Import-ExchangeCertificate command installs the certificate in the PFX file on the target server together with its private key. The content of the file is read through the UNC path with [System.IO.File]::ReadAllBytes and passed to the -FileData parameter. The -Server parameter takes the server that the certificate will be installed on, and -Password takes the password that I set during the export.

The -PrivateKeyExportable:$true value allows the certificate to be exported again from EXCHSRV02 together with its private key. If you do not plan to move the certificate from this server to another server later, leaving the value as $false prevents the private key from being taken off this server.


Import-ExchangeCertificate -Server "EXCHSRV02" -FileData ([System.IO.File]::ReadAllBytes('\\EXCHSRV01\C$\CERT\Wildcard_firatboyan.com.pfx')) -PrivateKeyExportable:$true -Password (ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force)

Exporting and Importing an SSL Certificate

Checkpoint
The thumbprint in the command output must be the same as the 74805915260BBD4C868543D8D84004639FFAE545 value that I noted in section 3. At this stage the Services field is still empty. I do the assignment in the next section.

The command above works for a single remote server. When I need to install the same certificate on multiple servers, I use the script below. For each server in the list, the script reads the same PFX file, performs the import and writes the returned thumbprint to the screen. Seeing the same thumbprint on every line shows that the same certificate was installed on all servers.

The server list is an example. Edit it for your own environment. I do not add the source server, where the certificate is already installed, to the list. In this example that server is EXCHSRV01. -ErrorAction Stop makes an error that occurs during the import go to the catch block.


$servers = @("EXCHSRV02", "EXCHSRV03")
$certFilePath = '\\EXCHSRV01\C$\CERT\Wildcard_firatboyan.com.pfx'
$certPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
foreach ($server in $servers) {
try{
Write-Host "Importing the certificate to $server..."
$importedCert = Import-ExchangeCertificate -Server $server -FileData ([System.IO.File]::ReadAllBytes($certFilePath)) -PrivateKeyExportable:$true -Password $certPassword -ErrorAction Stop
$Thumbprint = $importedCert.Thumbprint
Write-Host "The certificate was imported to $server successfully. Thumbprint: $Thumbprint"}
catch{
Write-Host "An error occurred while importing the certificate: $_"}}

6- Assigning Services to the Imported SSL/TLS Certificate

The imported certificate is in the certificate store of the server, but no Exchange service uses it. I assign the certificate to the services with the Enable-ExchangeCertificate command. I run the command remotely for EXCHSRV02.

I define the hostname of the target server in the $Servers variable and process it with ForEach. -Thumbprint specifies the certificate to assign, and -Services specifies the services that the certificate is assigned to. Here I assign the certificate to the SMTP and IIS services. By default, the IIS assignment also enables the SSL requirement on the Default Web Site. If you do not want this behaviour, you can add the -DoNotRequireSsl parameter to the command.

During the SMTP assignment, the command asks for confirmation to replace the default self-signed certificate, which encrypts SMTP traffic between servers, with the new certificate. In my lab environment I skip this question by using -Force and -Confirm:$False, so the default SMTP certificate is replaced with the new certificate. Microsoft states that this change is generally not required for internal SMTP traffic. If you want to keep the default certificate in a production environment, run the command without these 2 parameters and answer No to the replacement question. In this case the certificate is still enabled for SMTP, and the default certificate stays in place.

The -Force parameter in the command replaces the default SMTP certificate with the new certificate without asking for confirmation. Use this parameter only when you want to replace the default certificate.

$Servers = "EXCHSRV02"
$Servers | ForEach {Enable-ExchangeCertificate -Thumbprint 74805915260BBD4C868543D8D84004639FFAE545 -Services "SMTP, IIS" -Server $_ -Confirm:$False -Force}

Exporting and Importing an SSL Certificate

To do the same assignment on multiple remote servers, I use the script below. I add only the servers where I have just imported the certificate to the list. The source server EXCHSRV01, where the assignment already exists, is not in the list. Because the script runs non-interactively, it uses -Force, which means that it also replaces the default SMTP certificate on every server in the list.

The assignment command runs in the try block with -ErrorAction Stop. The success message is written only when the command completes without an error. If an error occurs, the catch block reports the server name and the error.


$servers = @("EXCHSRV02", "EXCHSRV03")
foreach ($server in $servers) {
try {
Enable-ExchangeCertificate -Thumbprint 74805915260BBD4C868543D8D84004639FFAE545 -Services "SMTP, IIS" -Server $server -Confirm:$False -Force -ErrorAction Stop
Write-Host "The certificate was enabled on $server."}
catch {
Write-Host "The certificate could not be enabled on $server: $_"}}

Below, I explain separately why I assigned the certificate to these 2 services.

6.1- IIS (Internet Information Services)

IIS is the layer where clients connect to Exchange Server over HTTPS. Outlook on the web (OWA), the Exchange Admin Center (EAC), ActiveSync, MAPI over HTTP, Exchange Web Services (EWS), Autodiscover and Offline Address Book (OAB) distribution run on IIS.

When this certificate is not assigned to IIS, the traffic is not left unencrypted, because the self-signed certificate that Exchange Setup installed is in use. The problem is on the trust side. Because clients do not trust a self-signed certificate automatically, users receive a certificate warning in OWA and in Outlook.

Because only 1 certificate can be bound to a website, all the DNS names that clients use to connect to these services must be in the certificate. Assigning the certificate to the IIS service removes the warnings if the certificate covers these names and the client trusts the CA that issued the certificate. Clients trust commercial CA certificates automatically. If an internal CA is used, the root certificate of the CA must be defined as trusted on the clients.

6.2- SMTP (Simple Mail Transfer Protocol)

SMTP carries email traffic between servers and the email exchange with external systems. Exchange Server can encrypt SMTP traffic with TLS by default because of the self-signed certificate that is installed during setup. The Exchange Servers in the organisation communicate with each other using this certificate.

A trusted certificate is required in scenarios where the other side validates the certificate. TLS connections with external systems that perform certificate validation and clients that connect over SMTP are the typical examples. In these connections, the certificate must contain the DNS name that the other side connects to. When I assign the certificate to the SMTP service, Exchange can present this certificate instead of the self-signed certificate in connections of this type.

6.3- Checking the IIS and SMTP Services

I verify that the assignment was made with the Get-ExchangeCertificate command. I run the command remotely with the -Server EXCHSRV02 parameter, so that the query returns the certificates on EXCHSRV02 and not the certificates on the local server.


Get-ExchangeCertificate -Server EXCHSRV02 | Where-Object { $_.Status -eq "Valid" -and $_.IsSelfSigned -eq $false } | Format-List FriendlyName, Subject, CertificateDomains, Thumbprint, Services, NotBefore, NotAfter

Exporting and Importing an SSL Certificate

Checkpoint
In the output, the thumbprint must be 74805915260BBD4C868543D8D84004639FFAE545, and IIS and SMTP must be listed in the Services field.

7- Restarting IIS (Internet Information Services)

IIS may need to be restarted after the Enable-ExchangeCertificate command. In the scenario that Microsoft describes, Exchange can continue to use the previous certificate to encrypt and decrypt the cookie used for OWA authentication. An IIS restart is recommended in environments that use Layer 4 load balancing. This operation does not affect the SMTP side, because the SMTP certificate is used by the Transport services.

I run the command remotely on the EXCHSRV02 server with Invoke-Command. iisreset /RESTART first stops the IIS services and then starts them again. The stop and start messages are produced by iisreset.exe itself, and this output is displayed without the -Verbose parameter as well.

During the IIS restart, the OWA, ActiveSync, MAPI over HTTP and EWS connections on this server are dropped. Do this outside working hours or after you remove the server from the load balancer pool.

Invoke-command -Computername "EXCHSRV02" -Scriptblock {iisreset /RESTART} -Verbose

Exporting and Importing an SSL Certificate

To do the same operation on multiple remote servers, I use the script below. The script goes through the servers in the list in order and restarts the W3SVC and WAS services on each one with Restart-Service. Restarting the services directly instead of using iisreset brings the operation under PowerShell error handling. Because of -ErrorAction Stop, a connection or permission error goes to the catch block. The break in the catch block stops the loop at the server where the error occurs and prevents the script from moving to the next server before the problem is resolved.

The fact that the services have restarted does not show that Exchange accepts requests over HTTPS again. For this reason, the script stops after every restart and waits for confirmation. Before I move to the next server, I open the /owa/healthcheck.htm page through the restarted server's own address and verify that I receive an HTTP 200 response. If there is no response, I do not type YES and the loop stops.

I add only the servers that I assigned the IIS certificate to in this operation to the list. The source server EXCHSRV01 is not in the list because its assignment did not change.


$servers = @("EXCHSRV02", "EXCHSRV03")
foreach ($server in $Servers) {
try {
Write-Host "Restarting IIS on $server..."
Invoke-Command -ComputerName $server -ErrorAction Stop -ScriptBlock {
Restart-Service -Name W3SVC, WAS -Force -ErrorAction Stop }
Write-Host "IIS was restarted successfully on $server."
if ((Read-Host "Did /owa/healthcheck.htm return HTTP 200 on $server? Type YES to continue") -ne 'YES') { break }}
catch {
Write-Host "IIS could not be restarted on $server: $_"
break}}

8- Verifying the SSL/TLS Certificate on the IIS Frontend Bindings

After the service assignment, I check that the certificate is reflected in the frontend bindings settings on IIS. On the Default Web Site in my environment, there are 2 bindings for the HTTPS protocol. The (*) IP address accepts connections that arrive at any IP address of the server. The 127.0.0.1 IP address is used for connections made from the server itself. The number of bindings can be different in your own environment, and the check applies to every HTTPS binding that is listed.

The purpose of this check is to see by thumbprint that the certificate presented to clients is the certificate that I expect. If I see a different thumbprint on a binding, it means that a different certificate is presented to the connections that arrive through that binding.

I run the command below remotely on the EXCHSRV02 server. The command queries the HTTPS bindings on the Default Web Site with Get-WebBinding and lists the IPAddress, Port and SslCertificate information for each one. The SslCertificate field is the thumbprint of the certificate bound to that binding.


Invoke-Command -ComputerName EXCHSRV02 -ScriptBlock {
Import-Module WebAdministration
Get-WebBinding -Name "Default Web Site" | Where-Object { $_.protocol -eq "https" } | ForEach-Object {
[PSCustomObject]@{IPAddress = $_.BindingInformation.Split(':')[0]
Port = $_.BindingInformation.Split(':')[1]
SslCertificate = $_.certificateHash } } }

Exporting and Importing an SSL Certificate

Checkpoint
The 74805915260BBD4C868543D8D84004639FFAE545 value must appear in the SslCertificate field of every binding that is listed. If the value is different, I run the assignment command in section 6 again for the IIS service.

This check is for the Default Web Site only. The binding on port 444 on the Exchange Back End site must stay with the Microsoft Exchange self-signed certificate that Exchange Setup installed. Do not change this binding.

9- Final Checks and Rollback

After all the steps, I run the Get-ExchangeCertificate query under the heading 6.3- Checking the IIS and SMTP Services once more for EXCHSRV02. I review for the last time that the FriendlyName and Subject information is the same as on the certificate on the source server, that the thumbprint has not changed, that the IIS and SMTP services are listed in the Services field, and the NotAfter date.

Exporting and Importing an SSL Certificate

After the verification on the command side, I connect to EXCHSRV02 with OWA from a client and check that no certificate warning appears. The fact that mail flow continues does not show on its own which certificate is in use. I see the certificate used for SMTP traffic between servers by running the Get-TransportService command in section 1 again and checking the InternalTransportCertificateThumbprint value. Because I used -Force in my lab environment, this value must be the thumbprint of the new certificate.

The point to know about rollback is this: a service that is assigned to a certificate with Enable-ExchangeCertificate cannot be removed from that certificate afterwards. To roll back, the service must be assigned to another certificate. For IIS, I assign the certificate that has the previous binding thumbprint, which I noted in section 1, to the IIS service again with Enable-ExchangeCertificate. If the default SMTP certificate was replaced, I enable the certificate that has the previous InternalTransportCertificateThumbprint value, which I noted, for SMTP again, confirm the replacement question and verify the result with Get-TransportService. Because the previous IIS certificate and the previous SMTP certificate can be different, the 2 values must be recorded separately.

When the operation is finished, I also delete the PFX file in the \\EXCHSRV01\C$\CERT folder.

This article walked through exporting an SSL/TLS certificate used in an Exchange Server environment in PFX format from the Exchange Management Shell, importing it to a new server with the -FileData parameter and assigning it to the IIS and SMTP services. I then verified by thumbprint that the assignment was reflected in the Services field and in the HTTPS bindings on the Default Web Site. With the scripts that apply the same steps to multiple servers, I also showed how to distribute the same certificate to all servers in multi-server environments.

I hope you find it useful...

No comments on this article yet. Be the first to comment!

750 characters available.
Security code
Comments are published once approved.
Your email is only used to let you know when your comment is approved. It is never published.