How to connect to Office 365's PowerShell in the cloud

If you’re familiar with Office 365, you know that you can perform administrative tasks through Office 365’s Web-based interface. Doing so executes one or more PowerShell commands under the covers. In Office 365, you can

    Requires Free Membership to View

also run those commands directly from a PowerShell session on your local computer. The problem is, many admins don’t know how to connect to PowerShell via Office 365 in the first place.

Connecting to PowerShell gives you complete control -- and plenty of automation -- for managing Office 365 resources. This is helpful because Microsoft is constantly adding new Office 365 administrative tasks to the collaboration suite.

Making that initial PowerShell connection isn’t immediately obvious unless you’ve got the code necessary to do so:

Set-ExecutionPolicy RemoteSigned
Import-Module MSOnline
$LiveCred = Get-Credential
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $O365Session

The first command allows for the execution of signed PowerShell scripts. The second command imports the Microsoft Online Service Module for Windows PowerShell into the current session.

Note: If you haven't already installed it, you need to install the Online Service Module first. You can download it via Microsoft’s Office 365 help website.

The third command generates a prompt for your Office 365 username and password; it will appear as a standard Windows dialog box. Assuming you have administrative privileges, enter your username (username@domain.onmicrosoft.com) and password. Those credentials are stored in the $LiveCred object for later use.

The fourth command creates a new PowerShell session that actually exists on your Office 365 servers. Since this is a remote session, the last two commands connect and import the remote session into your local session.

This whole process merges the remote session with your local session to create a single, unified management console. Through this session, you can manage resources both locally and inside Office 365’s cloud.

As you can imagine, these commands can get cumbersome to plug in each time you need to manage Office 365. Fortunately, you can automate the process by adding them to your $profile. After doing so, they will automatically execute each time you launch a PowerShell session.

If you don’t have a $profile , you Get one by creating a text file named Microsoft.PowerShell_profile.ps1 and saving it to the C:\Users\<userName>\Documents\WindowsPowerShell folder.

About the author:
Greg Shields, MVP, is a partner and principal technologist with Concentrated Technology. An IT industry analyst, author, speaker and trainer, you can find Greg at www.concentratedtech.com.

This was first published in April 2012

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.