Home » Exchange Server » PowerShell Function to Connect to Office 365 Exchange Online

PowerShell Function to Connect to Office 365 Exchange Online

Connecting to Exchange Online for an Office 365 tenant with PowerShell is a reasonably simple task. All you need is PowerShell on your computer, which is included by default in any recent version of Windows and Windows Server.

There are three basic steps for connecting to Exchange Online with PowerShell, and you’ll find these in official help documentation on TechNet as well as on many blogs. The steps are:

  • Capture credentials
  • Create a new PSSession
  • Import the PSSession

You can streamline this process by adding a custom function to your PowerShell profile. Here are two functions you can use for connecting and disconnecting from Exchange Online.

After adding the functions to your PowerShell profile open a new console session and run Connect-O365 to connect.

A dialog will popup asking for your Office 365 admin credentials. After a short wait the connection will be established and you can run cmdlets such as Get-Mailbox to list the mailbox users in Exchange Online for your Office 365 tenant.

To disconnect the session use the Disconnect-EXOnline function.

As an additional tip, if you’re running a hybrid configuration and using an Exchange Management Shell session to connect to Office 365 you may run into issues due to the same cmdlets being available in both your on-premises Exchange and Exchange Online. In that situation you can use Jeff’s tip of adding a prefix to the Import-PSSession command.

For example:

You might prefer to use a prefix of “Online” or “Cloud” instead. Really it is up to you. But when you do this the prefix is added to the cmdlets for your Office 365 session, so Get-Mailbox becomes Get-EXOMailbox instead.

Paul is a Microsoft MVP for Office Servers and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul is a co-author of Office 365 for IT Pros and several other books, and is also a Pluralsight author.
Category: Exchange Server

12 comments

  1. mm says:

    I’m no sure, but in disconnect function should be Remove-PSSession -Name “Office 365” not Remove-PSSession -Name “Exchange Online”

  2. Sashi Kanth says:

    Hi Paul,

    I need Customer GUID, Subscription GUID which were created by OrderManagement API (CreateCustomerAccount, PlaceOrder) with Office 365 PowerShell (either Exchange or MSOL) script.

    Is it possible?

    Thank you in advance.

    Regards,
    Sashi

  3. ahmad says:

    Hi Paul,

    thanks for the perfect article .

    can we use a non-global admin credentials(user management or service) admin ?
    I tried that from c# code and it’s seem that is not possible .

    thanks in advance .

  4. Will says:

    Hi Paul, I just created something similar for myself and I’m running PowerShell v3 on Windows 7 and find that the commands aren’t recognised after I connect. If I use the same commands outside of a PowerShell function, then the script works fine. It seems like the imported session isn’t available after the function has completed?

      • RM says:

        I’m having the same issue here. Commands work fine manually — All the remote commands import correctly including Exchange commands. But as a function, the MSOL commands are importing but not the Exchange commands (e.g. Get-MSOLuser works but Get-Mailbox is unrecognized).

  5. Kishore says:

    Hi Paul,

    Good Day!

    I’m trying to import all modules in a single PowerShell using the below Script. Everything is working fine but when I try to execute a command Get-User “Username” .. powershell is getting crashed and closing. Please help me..

    All modules here are :
    1. Exchange online powershell.
    2. Azure online
    3. On premise AD module
    4. Exchange 2010 on premise module.

    $SecureData = cat C:ScriptO365EnbaleDisablekishorecred.txt | convertto-securestring
    $365Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList “kishore@mydomain.com”,$SecureData
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $365Cred -Authentication Basic -AllowRedirection
    Import-PSSession $Session
    Import-Module ActiveDirectory
    . ‘C:Program FilesMicrosoftExchange ServerV14binRemoteExchange.ps1’ Connect-ExchangeServer -auto
    sleep -Seconds 10
    Connect-MsolService -Credential $365Cred
    Import-Module MSOnline
    Set-ExecutionPolicy RemoteSigned

    Thank you
    Kishore

  6. Doyle says:

    I know this blog is a year old but wondered if there was a simple option for connecting to EXO while using MFA. The process now is to go to Exchange Hybrid page of your EXO admin center and click ‘Configure’ under the Powershell module. This is the only module that supports MFA to EXO. This adds the ‘Microsoft Exchange Online Powershell Module’ to my start menu but it’s not possible to call Connect-EXOPSSession from my regular powershell. Now I have to open a specific shell and run my scripts through that. It would be nice to have that module available in regular windows shell. Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *