[Update 26/03/2013] I wanted to share a quick update to the script with you. Over the past few weeks, I have hitting an issue at some customers that used an outbound authenticating proxy which prevented me from connecting to Exchange Online using the functions from my profile. As such, I have tweaked the script a bit to now include a switch (-ProxyEnabled) that, when used, will trigger PowerShell to authenticate the session against the proxy.

The new script now looks like this:

[sourcecode language=”PowerShell”]
function Connect-ExchangeOnline{

$Session = New-Pssession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential (Get-Credential) -Authentication Basic -AllowRedirection -sessionOption (New-PsSessionOption -ProxyAccessType IEConfig -ProxyAuthentication basic)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Authentication Basic -AllowRedirection -Credential (get-credential)

Import-PSSession $session

function Disconnect-ExchangeOnline {
Get-PSSession | ?{$_.ComputerName -like "*outlook.com"} | Remove-PSSession

[Original Post]

Office 365 allows you to connect remotely to Exchange online using PowerShell. However, if you had to type in the commands to connect every time, you would be losing quite some time:

[sourcecode language=”powershell”]$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri <a href="https://ps.outlook.com/PowerShell">https://ps.outlook.com/PowerShell</a&gt; -Authentication Basic -Credential (Get-Credential) -AllowRedirection

Import-PSSession $session[/sourcecode]

Equally, to disconnect, you’d have to type in the following command each time

[sourcecode language=”powershell”]Get-PSSession  | ?{$_.ComputerName -like "*.outlook.com"} | Remove-PSSession[/sourcecode]

However, it is relatively easy to add both commands into a function which you can afterwards add them into your profile:

[sourcecode language=”powershell”]Function Connect-ExchangeOnline{
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri <a href="https://ps.outlook.com/powershell">https://ps.outlook.com/powershell</a&gt; -Authentication Basic -AllowRedirection -Credential (Get-Credential)
Import-PSSession $session

Function Disconnect-ExchangeOnline{
Get-PSSession  | ?{$_.ComputerName -like "*.outlook.com"} | Remove-PSSession

To add these functions to your PowerShell profile, simply copy-past them into your profile. To find out where your profile-file is located, type the following:

PS C:\> $profile

To start using the functions, all you have to do is to call them from the PowerShell prompt:

PS C:\> Connect-ExchangeOnline

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:

WARNING: Your connection has been redirected to the following URI:

PS C:\> Disconnect-ExchangeOnline

Note   there is no output for the Disconnect-ExchangeOnline function