Thursday, 17 July 2025

Certificate Based Authentication in Exchange Online PowerShell

 
# Create Self Signed certificate
$mycert = New-SelfSignedCertificate -DnsName "spscript.onmicrosoft.onmicrosoft.com" -CertStoreLocation "cert:\CurrentUser\My" -NotAfter (Get-Date).AddYears(5) -KeySpec KeyExchange


# Export certificate to .cer file
$mycert | Export-Certificate -FilePath c:\temp\mycert.cer

Connect-ExchangeOnline -Certificate "8D5519179561FFF91A0943BF2ED9357C13762FF0"


# Export certificate to .pfx file
$mycert | Export-PfxCertificate -FilePath mycert.pfx -Password (Get-Credential).password

Connect-ExchangeOnline `
-CertificateFilePath "C:\Users\navin\Desktop\automation-cert.pfx" `
-CertificatePassword (Get-Credential).password `
-AppID "36ee4c6c-0812-40a2-b820-b22ebd02bce3" `
-Organization "contosoelectronics.onmicrosoft.com"





--------------End----------------