Configuring a Windows PowerShell Profile for Office 365 Exchange Online

Introduction

This script can configure a Windows PowerShell profile for Office 365 Exchange Online. It appends the Connect-OSCExchangeOnline and Disconnect-OSCExchangeOnline functions to your Windows PowerShell profile. Afterward, you can use these two functions directly without remembering New-PSSession and its parameters.

Scenarios

Currently, Office 365 Exchange Online administrators use the New-PSSession function to connect Windows PowerShell to Office 365. Some administrators feel that it is difficult to remember all the parameters clearly. It is better to use a Windows PowerShell profile to store the two functions and help administrators use Office 365 more easily.

Script

This script contains the following function:

You can use this script in the following way.

  1. Open Windows PowerShell.
  2. Run Import-Module cmdlet to import this module file.
    Import-Module filepath\scriptname.psm1

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
#Check the existence of two functions$isAdded = Select-String-Path $profilePath-Pattern "[Connect|Disconnect]-OSCExchangeOnline"-Quiet 
$content = "`r`n`r`n$connectToOffice365`r`n`r`n$disconnectFromOffice365"if (-not $isAdded) { 
    Out-File -FilePath $profilePath-InputObject $content-Encoding "ascii"-Append 
    $verboseMsg = $Messages.RestartPowerShell 
    $pscmdlet.WriteVerbose($verboseMsg) 
} else { 
    $verboseMsg = $Messages.IsAdded 
    $pscmdlet.WriteVerbose($verboseMsg) 
}

Examples

Example 1: How to display help information about Set-OSCEXOProfile.
To display help information about Set-OSCEXOProfile, please run this command.
Get-Help Set-OSCEXOProfile -Full

Example 2: How to configure a Windows PowerShell profile for the current user and current host.
To configure a Windows PowerShell profile for the current user and current host, please run this command.
Set-OSCEXOProfile -Verbose

Example 3: How to configure a Windows PowerShell profile for all users and all hosts. 
To configure a Windows PowerShell profile for all users and all hosts, please run this command.
Set-OSCEXOProfile -ProfileType AllUsersAllHosts -Verbose

Note If you want to configure the Windows PowerShell profile on a utility server, please start Windows PowerShell with the Run as administrator option before you run the command. Use this option if the server will be accessed by multiple operators. After you run the command, each operator can use Connect-OSCExchangeOnline and Disconnect-OSCExchangeOnline.

Additional Resources

Technical Resources:
Windows PowerShell Advanced Function
about_Profiles 
Use Windows PowerShell in Exchange Online