Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 6, 2023 AzureExchangeMicrosoft 365PowerShell

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

Microsoft Entra ID (ex Azure AD) supports Certificate Based Authentication (CBA). This means that you can use certificate authentication to automatically run your PowerShell scripts with no password entry and without using Azure MFA. With certificate-based auth, you no longer need to worry about securely storing passwords in PowerShell scripts or using third-party vault storage through the SecretManagement module.

Here is how to use certificates for authentication from your PowerShell scripts against Microsoft Entra ID (Azure AD) and Exchange Online.

You must first create a certificate and install it on your computer. You can obtain a certificate from your CA or issue a self-signed certificate. In this example, we will create a self-signed certificate by using the New-SelfSignedCertificate cmdlet.

Generate a self-signed certificate with a 3-year validity period:

$certvalid = (Get-Date).AddYears(3)
$newcert = New-SelfSignedCertificate -DnsName "pre_prod.woshub.com" -CertStoreLocation "cert:\LocalMachine\My" -NotAfter $certvalid -KeySpec KeyExchange -FriendlyName "Azure Microsoft Entra ID PowerShell Auth cert"
$newcert|fl Subject,Thumbprint,NotBefore,NotAfter

Copy the value of the certificate thumbprint.

Create a self signed certificate for Azure

Then export the certificate to the CER and PFX files:

$newcert | Export-Certificate -FilePath "C:\PS\azure-auth.cer"
$newcert | Export-PfxCertificate -FilePath "C:\PS\azure-auth.pfx" -Password $(ConvertTo-SecureString -String "S3dPswrd@123" -AsPlainText -Force)

Now you need to create an application in Azure Microsoft Entra:

  1. Sign in to Azure Portal and go to Microsoft Entra ID -> App registrations -> New registration;
  2. Specify the app name (testCertAuthPowerShell), select Accounts in this organizational directory only (Single tenant), and click Register;
  3. Copy the Application (client ID) value; Create an App in Entra ID
  4. Then assign permissions for your app. Go to API permissions -> Add a permission. In this example, we will grant permissions to manage the Exchange Online configuration (APIs my organization uses -> Office 365 Exchange Online) Azure -> Application permissions -> Exchange.ManageAsApp -> Add permissions -> Grant admin consent); Assign API permissions for Azure app
  5. Then go to Certificates and secrets and upload a self-signed certificate file from your computer; Import certificate to Azure app registration
  6. Next, assign the Exchange Administrator role to your app. Go to Azure Active Directory -> Roles and administrators -> select the Exchange Administrator role -> Add Assignments -> Select your app from the list (testCertAuthPowerShell). Assig Exchange admin permissions for Azure Entra ID app

Once that’s done, you can try to connect to Exchange Online from a PowerShell script on your computer by using the certificate. Note that in this example, the Exchange Online PowerShell module is already installed on the computer.

You will need the values of the following variables (the ones you copied earlier):

$certThumbprint = "9CF05589A4B29BECEE6456F08A76EBC3DC2BC581"
$AzureAppID = "111111-2222-3333-4444-123456789"
$tenant="woshub.onmicrosoft.com"

Now you can connect to the Exchange Online tenant using the certificate:

Connect-ExchangeOnline -AppId $AzureAppID -CertificateThumbprint $certThumbprint -Organization $tenant

Using Exchange Online Certificate-Based Authentication with PowerShell

There will be no password prompt and no Modern Authentication form when you connect to Azure.

List mailboxes in your Exchange tenant:

Get-EXOMailbox

Remember to add a command to disconnect the Exchange Online session at the end of your PowerShell script:

Disconnect-ExchangeOnline -Confirm:$false

Similarly, you can configure certificate-based auth for other Microsoft 365 services as well. The important thing here is to properly assign permissions for your Azure app.

For example, if you are using the Teams PowerShell module, you would run the following command to authenticate with a certificate:

Connect-MicrosoftTeams -CertificateThumbprint $certThumbprint -ApplicationId $AzureAppID -TenantId $tenant

PowerShell: Microsoft Entra certificate-based authentication in Teams

To use the PowerShell Azure AD module with a certificate to connect to Microsoft Entra:

Connect-AzureAD -TenantId $tenant -ApplicationId $AzureAppID -CertificateThumbprint $certThumbprint

Learn more about creating apps in Azure, assigning permissions, and using the Microsoft Graph API in PowerShell.

Besides, you can import a certificate to another Windows host using a PFX file containing a password-protected private key. Use the Certificates MMC snap-in or PowerShell to import the certificate:

$password = ConvertTo-SecureString -String "S3dPswrd@123" -Force -AsPlainText
Import-PfxCertificate -Password $password -FilePath "C:\PS\azure-auth.pfx" -CertStoreLocation Cert:\CurrentUser\My

You can then use Azure certificate-based authentication in PowerShell scripts on that computer.

0 comment
3
Facebook Twitter Google + Pinterest
previous post
Reset Root Password in VMware ESXi
next post
Tracking Printer Usage with Windows Event Viewer Logs

Related Reading

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

How to Query and Change Teams User Presence...

October 8, 2023

Installing Language Pack in Windows 10/11 with PowerShell

September 15, 2023

Configure Email Forwarding for Mailbox on Exchange Server/Microsoft...

September 14, 2023

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Checking User Sign-in Logs in Azure AD (Microsoft 365)
  • How to Reset User Password in Azure Active Directory (Microsoft 365)
  • Enable or Disable MFA for Users in Azure/Microsoft 365
  • Configuring Azure AD Password Policy
  • Disable Welcome Message for Microsoft 365 Groups
  • How to Restore Deleted Users in Azure AD (Microsoft 365)
  • Manage Groups in Azure AD and Microsoft 365 Using PowerShell
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top