Create Your Own PowerShell Module For Exchange Online

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.

Export-PSSession

To do that, replace

Import-PSSession $Session

with

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).

Import-Module ExchangeOnline

Figure 1: Import-Module ExchangeOnline

IntelliSense in Exchange Online PowerShell Module

Figure 2: IntelliSense in Exchange Online PowerShell Module

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).

Credential Prompt for Implicit Remoting Session

Figure 3: Credential Prompt for Implicit Remoting Session

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.

3 thoughts on “Create Your Own PowerShell Module For Exchange Online

  1. Pingback: Create Your Own PowerShell Module For Exchange Online - How to Code .NET

  2. Amanda Debler

    Thanks for this post – it’s gotten me some of the way towards using implicitly-remoted modules with a custom module that enforces my company’s account rules (in this case, on-prem Skype for Business and Exchange).

    Have you found a way to use stored credentials (captured earlier in the session or from file) for that initial credential request that comes up when you run the first cmdlet in a session after importing the module (in your post, when you run Get-Mailbox after Import-Module ExchangeOnline)?

    Reply
    1. Muditha Jayath Chathuranga Post author

      Hi Amanda,

      You’re welcome and thank you for your nice comments. However, I haven’t had the need to use stored credentials for this particular instance. Therefore I’m afraid I cannot be more helpful to you in that question. If I find a way to do this, I will surely write an article about the way of doing it.

      Thank you.

      Reply

Leave a Reply