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 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:
$LiveCred = Get-Credential
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $LiveCred -Authentication Basic -AllowRedirection
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 (email@example.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.