Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Functions for connecting to and importing PowerShell sessions for Lync, Exchange (online and on-premise).
Function Test-MsolConnection {
A function that tests the Microsoft Online connection.
Tests to see if the Microsoft Online connection is available.
Author: Information Services
try {
Get-MsolCompanyInformation -ErrorAction Stop | Out-Null
Write-Verbose 'Microsoft Online connection test success.'
return $true
catch [Microsoft.Online.Administration.Automation.MicrosoftOnlineException] {
Write-Verbose 'Microsoft Online connection test failure.'
return $false
function Connect-ExchangeOnline {
A function that connects to the Exchange online system.
.Parameter Credential
A credential object with rights to connect to the Exchange server. This parameter is mandatory.
.Parameter AllowClobber
Boolean value of whether the imported session will clobber existing commands. Defaults to True.
.Parameter Prefix
Use this switch to prefix 'EOL' onto any imported cmdlets for Exchange Online.
This switch will prefix 'Compliance' onto imported cmdlets for the Compliance centre.
Asks for credentials and connects to Exchange Online.
$mycreds = Get-Credential
Connect-ExchangeOnline -Credential $mycreds
Connects to Exchange Online with the credentials in $mycreds.
Author: David Green
Param (
ParameterSetName = 'Prefix'
$Connection = 'EOL'
$Uri = switch ($Connection) {
'EOL' { '' }
'Compliance' { '' }
try {
$Splat = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionUri = $Uri
Credential = $Credential
Authentication = 'basic'
AllowRedirection = $true
if ($Prefix) {
$Splat.Add('Prefix', $Connection)
$null = Import-Module (Import-PSSession (New-PSSession @Splat)) -Global -ErrorAction Stop
catch {
Write-Error "Cannot connect or invalid credentials. Please try again."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment