How to Connect to Office 365 and Exchange Online in Less Than 2 Minutes using 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:
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
- At the root of your C drive ( C: ), create a folder named “Office365”.
- 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-CredentialConnect-MsolService –Credential $credential$Session = New-PSSession -ConfigurationName Microsoft.Exchange –ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication Basic –AllowRedirection
- In the same “Office365” folder, create another file named “ConnectTo.ps1” and copy/paste the following line into it:PowerShell -NoExit –File “C:Office365HowToConnect.ps1” 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).
- Please verify that you have the same folder/file tree as below:
- 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
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:
- Azure Active Directory Module for PowerShell installed. [Office 365]
- Disable the restricted PowerShell execution policy. [Office 365 and Exchange Online]
For more information or assistance with Office 365 or Exchange Online feel free to contact FMT Consultants.
Ouissame Bekada, Senior Consultants