Connect to Exchange Online using Powershell

How to connect to Exchange Online PowerShell.

The main reason you will want to connect to Exchange Online PowerShell is to run specific commands to change attributes and enable/disable services.

So how to connect, first of all you have to run powershell on a Windows 10/11 pc as administrator.

After you reach the powershell command line the commands needed are as follows.

  • First of all we have to install Exchange Online Poweshell Module (Execute the following commands.)
Set-ExecutionPolicy RemoteSigned 

- Execution Policy Change The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the aboutExecutionPolicies help topic at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y

Install-Module PowershellGet -Force

- NuGet provider is required to continue PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\administrator.EXOIP\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now? [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y

Install-Module -Name ExchangeOnlineManagement

- Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y

  • Then connect to Exchange Online (Execute the following commands.)
Connect-ExchangeOnline -UserPrincipalName admin@test.com

- Where admin@test.com user your admin account and connect using the pop up window.

  • Verify the connection by listing all mailboxes in the account (Execute the following command.)
Get-EXOMailbox | ft UserPrincipalName,DisplayName
  • Don't forget to disconnect. (Execute the following command.)
Disconnect-ExchangeOnline

This is the easiest way to connect to Exchange Online to setup smtp auth or what ever else you need.

Happy coding.

George

More from Georgios Papadakis
All posts