How I connect to Exchange Online with PowerShell


Nearly five years ago, I wrote a post describing how to connect to Exchange 2010 with PowerShell. That post remains very popular, which indicates that lots of people still seek help to understand how to accomplish the task. Technology has moved on and while the Exchange 2010 post remains valid for Exchange 2013, many companies now use Exchange Online and Office 365. However, PowerShell still remains the best way to accomplish many tasks, even if the data you’re now working with is in the cloud.

I get questions about how to connect to Exchange Online all the time, so this post provides a brief overview of what I do.

First, many blogs and other sources state that you have to install the Microsoft Online Services Sign-in Assistant RTW. I have run PCs with and without this component being installed and have not noticed a difference. I’m sure that I am overlooking something, but the lack of the software doesn’t get in the way of what I do.

Next, if you don’t have a PowerShell profile, we need to create one. The profile is used to load in commands that you commonly want to use in PowerShell sessions and is stored in a text file pointed to by the $Profile variable. Usually, the profile is stored in a file called \WindowsPowerShell\Microsoft.PowerShell_profile1.ps1 under the My Documents folder. The PS1 extension tells you that the profile is no more than a PowerShell script that is automatically invoked at the start of each session.

In any case, if you don’t have a profile, you can create one by running the PowerShell command:

[PS] C:> New-Item -Type File -Force $Profile

Once the file exists, you can edit it with NotePad by using the command:

[PS] C:> NotePad $Profile

Running the Connect-ExchangeOnline function

Running the Connect-ExchangeOnline function

Now to the commands in the profile. I have a function called Connect-ExchangeOnline that contains the following commands:

  1. Enter my Office 365 account credentials (Get-Credential).
  2. Connect to Exchange Online using the credentials that have been entered. The connection point is https://outlook.office365.com/PowerShell-liveId/. The DisableNameChecking parameter is used to suppress error messages that would otherwise result if cmdlets with the same name already exist in the session, which could happen if you also connect to the Office 365 Compliance Center with PowerShell
  3. Import the remote PowerShell session connected to Exchange Online. This will load in the cmdlets needed to manage Exchange Online and make them available.
  4. Import the MSOnline module. This is to allow me to manage Azure Active Directory (AAD objects. To manage AAD, you need to have the Azure Active Directory Module for Windows PowerShell installed on your PC.
  5. Some other commands are commented out. These are the commands necessary to import the Rights Management Service (RMS) module and Azure Active Directory Rights Management (AADRM) module and connect to the AADRM service. I don’t use these commands all the time but keep them in the profile as a reminder of what I need to do if I need to work with Rights Management.

function Connect-ExchangeOnline
{
$global:O365Cred = Get-Credential
$global:Session365 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $O365Cred -Authentication Basic -AllowRedirection
Import-PSSession $global:Session365 -DisableNameChecking
Import-Module MsOnline
Connect-MSOlService -Credential $O365Cred


#  Import-Module RMSProtection
#  Import-Module AADRM
#  Connect-AADRMService -Credential $O365Cred
}

And that’s about it. The screen shot below shows that I can interact with Exchange Online as if I were connected to an on-premises server. The available set of cmdlets is much smaller because Exchange Online doesn’t make cmdlets like those needed for server or database management available (Microsoft does this work for you).

Running Exchange Online cmdlets after a connection is made

Running Exchange Online cmdlets after a connection is made

As to the clock running in the title bar? Well, I’ve used this function (which I found somewhere on the net) for years. Here it is:

function Add-Clock {
$code = {
$pattern = ‘\d{2}:\d{2}:\d{2}’
do {
$clock = Get-Date -Format ‘HH:mm:ss’
$oldtitle = "Tony's PowerShell Console"
if ($oldtitle -match $pattern) {
$newtitle = $oldtitle -replace $pattern, $clock
} else {
$newtitle = “$clock $oldtitle”
}
[System.Console]::Title = $newtitle
Start-Sleep -Seconds 1
} while ($true)
}
$ps = [PowerShell]::Create()
$null = $ps.AddScript($code)
$ps.BeginInvoke()
}
Add-Clock

Have fun!

Follow Tony @12Knocksinna

Advertisements

About Tony Redmond ("Thoughts of an Idle Mind")

Exchange MVP, author, and rugby referee
This entry was posted in Cloud, Office 365 and tagged , , , . Bookmark the permalink.

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