How to connect to Office 365(Exchange Online) using Windows Powershell

     1.  Software Requirements

Install the 64-bit version of the Microsoft Online Services Sign-in Assistant: Link

Install the 64-bit version of the Windows Azure AD Module for Windows PowerShell: Link

2.  Retreive the credential from a user with sufficient permission (Global admin for example)

# Update the
credential with the appropriate UPN (UserName)

$credential = Get-Credential
-credential
GlobalAdmin@Tenant.onmicrosoft.com

3.  Import Office 365 Module

# Import the Office
365 module

Import-Module MsOnline

4.  Connect to office 365

# Connect to office
365 using the Credential previous provided

Connect-MsolService
-Credential
$credential

5.  Create and import Exchange PS  ssession

# Create a PS
Session to Exchange Online

$ExchangeModule=New-PSSession
-ConfigurationName Microsoft.Exchange -ConnectionUri “
https://ps.outlook.com/powershell” -Credential $Credential -Authentication “basic” -AllowRedirection

# Import the PS
Session Previously (allowing use of previous PS session with same Name)

 Import-PSSession $ExchangeModule -DisableNameChecking
-AllowClobber

 

Please find how to use this script:

 Please find a copy of the script:Connect_to_O365.ps1

Option 1: Launch the script while need to
connect to office 365

Before please be sure the requirements are set (
Softwares and Execution policy)

 image001

Option 2: you can update your Powershell Profile with a function

 

1.  Create a function

 

Function Connect-ExchangeOnline {

# Update the credential with the appropriate UPN (UserName)

$credential = Get-Credential -credential lobalAdmin@tenant.onmicrosoft.com

# Import the Office 365 module

Import-Module MsOnline

# Connect to office 365 using the Credential previous provided

Connect-MsolService -Credential $credential

# Create a PS Session to Exchange Online

$ExchangeModule = New-PSSession -ConfigurationName Microsoft.Exchange`

-ConnectionUri https://ps.outlook.com/powershell -Credential $Credential `

-Authentication “basic” -AllowRedirection

# Import the PS Session Previously

# (allowing use of previous PS session with same Name)

Import-PSSession $ExchangeModule -DisableNameChecking -AllowClobber

}

2.  Open your Powershell Profile

a.  Open windows Powershell

b.  Type $Profile this will give the location of your
powershell profile:

 image002

3.  open the Microsoft.Powershell_Profile.ps1 with any
text editor ( in this case Notepad)

 image003

d.  Copy the function and save the change

 

3.  Re-open Windows Powershell then type the function
name Connect-ExchangeOnline

image004

Provide your Office 365 Tenant Admin Credential and now you are
connect to Exchange Online using Windows Powershell.

 

 image005

 

 

Author: John

I m a consultant who love Microsoft Exchange and try to learn as much as possible. so if you have any question related to Exchange please do not hesitate to ask. if I don't know I will know.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s