Many Office 365 administrators prefer to use PowerShell in their day-to-day administration tasks as it offers much more control in their environment over the GUI. When you’re connecting to Exchange Online from PowerShell, it uses a technique called PowerShell Implicit Remoting. Folks who are familiar with PowerShell know what Implicit Remoting is. Implicit Remoting is when you import a module from a remote computer.
Importing a module from a remote computer does not add the commands in the module to your local session. Instead, what it adds to your session are proxy commands. The proxy commands are functions that look like local cmdlets in the session. When you run a proxy command, instead of running the command on the local computer, the proxy runs the real command in a session on the remote computer and returns the results to the local session.
Typically, an administrator would use a script similar to below to connect to Exchange Online.
$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Import-PSSession $Session
Once you’re done with your work and when the session ends, the cmdlets are also gone from the system. Some of the administrators who script and automate their work, would find it unnecessary to connect to Exchange Online just to make the cmdlets available in their whenever they want to script.
There’s a much easier way to work with Exchange Online cmdlets. By using below cmdlet, we can export the list of cmdlets and create a PowerShell cmdlets module.
To do that, replace
Export-PSSession $Session -OutputModule ExchangeOnline
ExchangeOnline is the name you want to give out to your PowerShell module.
$UserCredential = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection Export-PSSession $Session -OutputModule ExchangeOnline
Next time when you open PowerShell or PowerShell ISE, it’s just matter of importing the module you’ve created earlier to the session or to the script and executing cmdlets (figure 1). This will be very easier when you’re creating scripts since the cmdlets are available offline and IntelliSense will also work properly without needing you to connect to Exchange Online first (figure 2).
Although the cmdlets are available locally, obviously you will have to connect to Exchange Online in order to execute these cmdlets. The first cmdlet you’re running will prompt you to enter your credentials to authenticate to Exchange Online for a new implicit remoting session (figure 3).
Although now you have a PowerShell module offline locally with you, it is best to update the module every once in a while. Reason is, Microsoft is frequently adding and removing cmdlets in the cloud. If you do not update the module, some of these cmdlets won’t work or won’t be available to you.