Connect to Office 365 Using a PowerShell Function

by Bharat Suneja on February 16, 2012

When you sign-up for Exchange Online (or Office 365, which includes Exchange Online), an Exchange organization is created for you in Microsoft’s datacenter.

One of the really cool things about Exchange Online is that besides its easy-to-use web interface, you also have the ability to manage your cloud-based organization using PowerShell. Shell-savvy administrators can run Exchange Management Shell commands against the service. This gives you unprecedented remote management capabilities for your cloud-based organization. If you’re connecting your on-premises Exchange Organization to your cloud-based organization in what’s known as a hybrid topology, you can seamlessly manage your on-premises and cloud-based organizations from the Shell.

You don’t need to have any Exchange management tools installed on your computer when connecting to Exchange Online or an on-premises Exchange server using a remote PowerShell session. All you need is Windows PowerShell 2.0 and Windows Remote Management (WinRM), which are installed by default on Windows 7 and Windows 2008 R2.
 
Still on an older OS? You can install Windows PowerShell and WinRM on Windows XP SP3, Windows 2003 SP2, Windows 2008 SP1 and Windows Vista SP1 or later – see Install and Configure Windows PowerShell.

For details about managing your cloud-based organization using the Shell, see Connect Windows PowerShell to the Service. It includes a set of commands you can use to connect to the PowerShell endpoint in Exchange Online. You can save the commands with in a PowerShell script (simply copy them to notepad or other text editor and save the file with a .ps1 extension) and connect to your cloud-based organization with a single-click.

I find it even more convenient to add it as a function in my PowerShell profile. It allows me to connect to Exchange Online using a single command which can be completed using tab completion in the Shell.

  1. Open your PowerShell profile (Microsoft.PowerShell_profile.ps1). The default path is C:\Users\<user name>\Documents\WindowsPowerShell\.

    Don’t have a PowerShell Profile?

    • A PowerShell profile file and the WidnowsPowerShell folder are not created by default. Type $Profile in PowerShell to see the default path where it expects the file to reside.
    • If you’ve never created a PowerShell profile, you can fire up Notepad or your favorite text editor (my favorite is Notepad++), paste the function from the next step and save the file as Microsoft.PowerShell._profile.ps1 in the default path returned by the $Profile variable.
  2. Paste this code at the bottom (or anywhere in the profile file but not within an existing function)

    function EXO {
    $LiveCred = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
    Import-PSSession $Session
    }
    function noEXO {Get-PSSession | ? {$_.computername -like “*.outlook.com”} | remove-pssession}

Now start a new PowerShell session and type EXO (you can also type the letter e and use tab completion), enter your Exchange Online credentials and in a few seconds you’ll be managing your cloud-based organization.

When you’re done, type noEXO to terminate the session.

You can also have a single PowerShell profile file stored in your OneDrive and sync it to all your computers. See Synchronize your PowerShell Profile with OneDrive.

{ 2 comments… read them below or add one }

Jeff Sullivan February 16, 2012 at 1:09 pm

Awesome post – thanks for this cool tip!

Reply

Brian Desmond February 16, 2012 at 2:12 pm

Leave a Comment

Previous post:

Next post: