Please refer to the following steps to connect to Office 365 using PowerShell
- Go to Office 365 DEMO and sign up for 90 Days Trial Account if you are Microsoft Partner
 - Download Microsoft Online Services Sign-in Assistant for IT Professionals RTW-msoidcli_64.msi and install it
 - Run “Install-Module MSOline” to install Windows Azure AD Module
 - Prepare the saved Credential for automate login in the future
 
$UserName = "[email protected]"
"XXXXX" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\scripts\Password.txt"
$Password = Get-Content "C:\Scripts\Password.txt" | ConvertTo-SecureString
#Specify Credential with password 
$Credential = new-object -typename System.Management.Automation.PSCredential -ArgumentList $UserName,$Password
#Login to Office 365
Connect-MsolService -Credential $Credential
Connect to Exchange Online 
We can login to Exchange Online using
#Change the Execution Policy to RemoteSigned 
Set-ExecutionPolicy RemoteSigned -Force #First Time only 
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
-Credential $Credential -Authentication  Basic -AllowRedirection
Import-PSSession $Session
Disconnect the session once you had completed your tasks
- Remove-PSSession $Session to Disconnected from Exchange-Online
 - Close the PowerShell to disconnect from msol
 
Reference Link
1. Connect to Office 365 PowerShell
