Connect to Office 365 and Exchange Online in Less than two Minutes PowerShell
90% of our clients type the following command lines highlighted in yellow each time they want to connect to Office 365 or Exchange Online via PowerShell:

Office 365

Office 365 Powershell

Exchange Online

Exchange Online Powershell

 

If you regularly need to connect to Office 365 and Exchange Online either because the GUI doesn’t allow you to perform a particular task or because the feature you want to leverage is not available through the GUI, please follow the steps below to make the simultaneous connection to Office 365 and Exchange Online as simple and fast as a double click:

Connect Simultaneously to Office 365 and Exchange Online

    1. At the root of your C drive ( C:\ ), create a folder named “Office365”.
    2. Within the “Office365” folder that you have just created, create a file named “HowToConnect.ps1” (the file name is up to you but the .ps1 extension is required) and copy/paste the following 5 command lines into the file.

      Import-Module MsOnline$credential = Get-Credential

      Connect-MsolService –Credential $credential

      $Session = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication Basic –AllowRedirection

      Import-PSSession $Session

      HowToConnect Office365 Exchange PowerShell

 

    1. In the same “Office365” folder, create another file named “ConnectTo.ps1” and copy/paste the following line into it:

      PowerShell -NoExit –File “C:\Office365\HowToConnect.ps1”PowerShell HowToConnect You might wonder why we need to create a second file…quick answer: it is a simple way to prevent the HowToConnectTo.ps1 script from closing after being run (default PowerShell behavior).

    2. Please verify that you have the same folder/file tree as below:

Office365 Folder

  1. Double click on ConnectTo.ps1 file, enter your Office 365 global administrator credentials and after a few seconds you should be connected to Office 365 and Exchange Online through the same PowerShell console.

To test it, run the following 2 command lines:

  • Get-MsolUser #Return the list of your Office 365 users
  • Get-Mailbox #Return the list of your Exchange Online mailboxes

Get-MsolUserGet-Mailbox

Note:
If you have never connected to Office 365 or Exchange Online before and you are encountering errors while going through the above step-by-step procedure, you will need to make sure you meet the following requirements:

For more information or assistance with Office 365 or Exchange Online feel free to contact FMT Consultants.

Written by:

Ouissame Bekada, Senior Consultants
FMT Consultants