Pro-Exchange,Lync & Office 365
Belgian Microsoft Unified Communications Professionals
Microsoft Exchange Server, Microsoft Lync Server & Office 365
Updated: script to connect to PowerShell in Exchange Online

[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:

function Connect-ExchangeOnline{
    [CmdLetBinding()]
    param(
        [Parameter(Position=0,Mandatory=$false)]
        [Switch]
        $ProxyEnabled
    )

    if($ProxyEnabled){
        $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)
    }
    else{
        $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:

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/PowerShell -Authentication Basic -Credential (Get-Credential) –AllowRedirection
Import-PSSession $session

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

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:

Function Connect-ExchangeOnline{
   $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
}

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 C:\Users\Michael\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

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: Credential
WARNING: Your connection has been redirected to the following URI: "https://pod51014psh.outlook.com/PowerShell-LiveID?PSVersion=3.0 "

PS C:\> Disconnect-ExchangeOnline

Note there is no output for the Disconnect-ExchangeOnline function


Posted 03-26-2013 11:09 by Michael Van Horenbeeck