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.

Function Connect-EXOnline {

    $URL = "https://ps.outlook.com/powershell"
    
    $Credentials = Get-Credential -Message "Enter your Office 365 admin credentials"

    $EXOSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $Credentials -Authentication Basic -AllowRedirection -Name "Exchange Online"

    Import-PSSession $EXOSession

}

Function Disconnect-EXOnline {
    
    Remove-PSSession -Name "Exchange Online"

}

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

PS C:UsersPaul> Connect-EXOnline

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.

PS C:UsersPaul> Connect-EXOnline
WARNING: Your connection has been redirected to the following URI:
"https://pod51055psh.outlook.com/powershell-liveid?PSVersion=4.0 "
WARNING: The names of some imported commands from the module 'tmp_g5b5ehu0.amo' include unapproved verbs that might
make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the
Verbose parameter. For a list of approved verbs, type Get-Verb.

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0        tmp_g5b5ehu0.amo                    {Add-O365AvailabilityAddressSpace, Add-O365DistributionGro...


PS C:UsersPaul> Get-Mailbox

Name                      Alias                ServerName       ProhibitSendQuota
----                      -----                ----------       -----------------
Alan.Reid                 Alan.Reid            sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Alannah.Shaw              Alannah.Shaw         sinpr04mb012     49.5 GB (53,150,220,288 bytes)
Bernadette.Hemming        Bernadette.Hemming   hkxpr04mb360     49.5 GB (53,150,220,288 bytes)
Bernadette.O'Connell      Bernadette.O'Connell sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Debbie.Dalgliesh          Debbie.Dalgliesh     sinpr04mb121     49.5 GB (53,150,220,288 bytes)
Debbie.Lisa               Debbie.Lisa          sinpr04mb364     49.5 GB (53,150,220,288 bytes)
DiscoverySearchMailbox... DiscoverySearchMa... sixpr04mb015     50 GB (53,687,091,200 bytes)
Ivana.Ferrary             Ivana.Ferrary        sixpr04mb191     49.5 GB (53,150,220,288 bytes)
Jack.Hirschorn            Jack.Hirschorn       hkxpr04mb134     49.5 GB (53,150,220,288 bytes)
Kathy.Abdullahi           Kathy.Abdullahi      sinpr04mb329     49.5 GB (53,150,220,288 bytes)
Kavita.Sheikh             Kavita.Sheikh        sinpr04mb090     49.5 GB (53,150,220,288 bytes)
Melanie.Thomas            Melanie.Thomas       sixpr04mb352     49.5 GB (53,150,220,288 bytes)
Merle.Elam                Merle.Elam           sixpr04mb0382    49.5 GB (53,150,220,288 bytes)
admin                     admin                sixpr04mb048     49.5 GB (53,150,220,288 bytes)
Rowena.Khan               Rowena.Khan          hknpr04mb257     49.5 GB (53,150,220,288 bytes)
Rupinder.Zaveri           Rupinder.Zaveri      sinpr04mb267     49.5 GB (53,150,220,288 bytes)
Theresa.Bolt              Theresa.Bolt         hknpr04mb226     49.5 GB (53,150,220,288 bytes)
Tina.Miller               Tina.Miller          sixpr04mb189     49.5 GB (53,150,220,288 bytes)

To disconnect the session use the Disconnect-EXOnline function.

PS C:UsersPaul> Disconnect-EXOnline

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:

Function Connect-EXOnline {

    $URL = "https://ps.outlook.com/powershell"
    
    $Credentials = Get-Credential -Message "Enter your Office 365 admin credentials"

    $O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $URL -Credential $Credentials -Authentication Basic -AllowRedirection

    Import-PSSession $O365Session -Prefix "EXO"

}

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.

PS C:UsersPaul> Get-EXOMailbox

Name                      Alias                ServerName       ProhibitSendQuota
----                      -----                ----------       -----------------
Alan.Reid                 Alan.Reid            sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Alannah.Shaw              Alannah.Shaw         sinpr04mb012     49.5 GB (53,150,220,288 bytes)
Bernadette.Hemming        Bernadette.Hemming   hkxpr04mb360     49.5 GB (53,150,220,288 bytes)
Bernadette.O'Connell      Bernadette.O'Connell sixpr04mb240     49.5 GB (53,150,220,288 bytes)
Debbie.Dalgliesh          Debbie.Dalgliesh     sinpr04mb121     49.5 GB (53,150,220,288 bytes)
Debbie.Lisa               Debbie.Lisa          sinpr04mb364     49.5 GB (53,150,220,288 bytes)
DiscoverySearchMailbox... DiscoverySearchMa... sixpr04mb015     50 GB (53,687,091,200 bytes)
Ivana.Ferrary             Ivana.Ferrary        sixpr04mb191     49.5 GB (53,150,220,288 bytes)
Jack.Hirschorn            Jack.Hirschorn       hkxpr04mb134     49.5 GB (53,150,220,288 bytes)
Kathy.Abdullahi           Kathy.Abdullahi      sinpr04mb329     49.5 GB (53,150,220,288 bytes)
Kavita.Sheikh             Kavita.Sheikh        sinpr04mb090     49.5 GB (53,150,220,288 bytes)
Melanie.Thomas            Melanie.Thomas       sixpr04mb352     49.5 GB (53,150,220,288 bytes)
Merle.Elam                Merle.Elam           sixpr04mb0382    49.5 GB (53,150,220,288 bytes)
admin                     admin                sixpr04mb048     49.5 GB (53,150,220,288 bytes)
Rowena.Khan               Rowena.Khan          hknpr04mb257     49.5 GB (53,150,220,288 bytes)
Rupinder.Zaveri           Rupinder.Zaveri      sinpr04mb267     49.5 GB (53,150,220,288 bytes)
Theresa.Bolt              Theresa.Bolt         hknpr04mb226     49.5 GB (53,150,220,288 bytes)
Tina.Miller               Tina.Miller          sixpr04mb189     49.5 GB (53,150,220,288 bytes)

About the Author

Paul Cunningham

Paul is a former Microsoft MVP for Office Apps and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul no longer writes for Practical365.com.

Comments

  1. Dheerendra Singh

    Hi,

    Connect-MsolService’ is not recognized in poweshell, newly setup kindly help to fix it.

  2. Jan

    Hi Paul,
    Thanks for the great blog, it saved me a lot of troubles and time!
    I’m trying to stay away from MS as much as possible because of it’s painful administration but your blog makes it much less painful!
    Thanks again.

  3. Ashley poxon

    Hi Paul,

    This is a really excellent article and works like a charm.

    I was wondering, if you had 20 domains in O365 and you then added 3 users and used this script, that would be perfect. However, when a 4th person is on-boarded to O365 \ Exchange Online, and they need the 20 domains adding as smtp addresses, how would you use the script for just one user?

  4. Kevin

    I am using Office 365 Small Bus. Premium and only know how to use the online UI ‘Exchange Admin Center’. How can I control the sent items behavior for shared mailboxes when delegates send mail as the mailbox, or on behalf of the mailbox using the web-based UI? I launch it via https://outlook.office365.com/ecp/

  5. Doyle

    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?

    1. Cris Burke

      I would like to do the same thing.

    2. Avatar photo

      They aren’t shipping it as a separate module right now, so the short answer is, no.

      Presumably one day they will ship it as a module.

      Long answer, when you have it loaded you can work out the temp folder path where it’s been loaded from, copy those files to another location, and then you can reference that in scripts or whatever you need. Downside, it won’t auto-update the module each time you run it.

  6. Kishore

    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

  7. Will

    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?

      1. RM

        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).

  8. ahmad

    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 .

  9. Sashi Kanth

    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

  10. mm

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

Leave a Reply