top of page

Comprehensive Overview and Samples of the PowerShell ExchangeOnline Module

  • Writer: Scott Head
    Scott Head
  • Nov 12
  • 3 min read

Managing Exchange Online effectively requires tools that provide both power and flexibility. The PowerShell ExchangeOnline module is one such tool, designed to help administrators automate tasks, manage mailboxes, and configure settings in Microsoft 365 environments. This post offers a clear overview of the module and practical examples to help you get started or improve your existing workflows.


The ExchangeOnline PowerShell module replaces older methods of connecting to Exchange Online, providing a more streamlined and secure experience. It supports modern authentication and offers a wide range of cmdlets tailored for Exchange Online management.


What Is the ExchangeOnline PowerShell Module?


The ExchangeOnline PowerShell module is a set of cmdlets that allow administrators to connect to and manage Exchange Online through PowerShell. Unlike the older remote PowerShell sessions, this module uses a dedicated connection method that improves performance and security.


Key features include:


  • Support for modern authentication methods, including multi-factor authentication (MFA)


  • Access to all Exchange Online management cmdlets


  • Simplified connection process with `Connect-ExchangeOnline`


  • Ability to manage mailboxes, policies, transport rules, and more


This module is essential for administrators who want to automate repetitive tasks or manage Exchange Online at scale.


Installing and Connecting to Exchange Online


Before using the module, you need to install it and establish a connection to your Exchange Online environment.


Installation Steps


  1. Open PowerShell as an administrator.


  2. Run the following command to install the module from the PowerShell Gallery:


```powershell

Install-Module -Name ExchangeOnlineManagement

```


  1. If prompted to trust the repository, type `Y` and press Enter.


Connecting to Exchange Online


Once installed, connect using:


```powershell

Connect-ExchangeOnline -UserPrincipalName your.email@domain.com

```


This command prompts for credentials and supports MFA. After successful authentication, you can run Exchange Online cmdlets.


To disconnect, use:


```powershell

Disconnect-ExchangeOnline -Confirm:$false

```


Common Cmdlets and Their Uses


The module includes many cmdlets. Here are some of the most useful ones with examples.


Managing Mailboxes


  • Get-Mailbox: Retrieves mailbox details.


```powershell

Get-Mailbox -Identity user@domain.com

```


  • Set-Mailbox: Modifies mailbox settings.


```powershell

Set-Mailbox -Identity user@domain.com -EmailAddresses @{Add="alias@domain.com"}

```


  • New-Mailbox: Creates a new mailbox (requires appropriate permissions).


Managing Mailbox Permissions


  • Add-MailboxPermission: Grants permissions to a mailbox.


```powershell

Add-MailboxPermission -Identity user@domain.com -User admin@domain.com -AccessRights FullAccess

```


  • Remove-MailboxPermission: Removes permissions.


Managing Distribution Groups


  • Get-DistributionGroup: Lists distribution groups.


```powershell

Get-DistributionGroup

```


  • Add-DistributionGroupMember: Adds a member to a group.


```powershell

Add-DistributionGroupMember -Identity "Sales Team" -Member user@domain.com

```


Managing Mail Flow Rules


  • Get-TransportRule: Retrieves mail flow rules.


```powershell

Get-TransportRule

```


  • New-TransportRule: Creates a new mail flow rule.


Practical Examples


Here are some practical examples to illustrate how the module can be used.


Example 1: Export Mailbox Sizes


To get mailbox sizes for all users and export the data to a CSV file:


```powershell

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics |

Select DisplayName, TotalItemSize, ItemCount |

Export-Csv -Path "MailboxSizes.csv" -NoTypeInformation

```


This command helps administrators monitor mailbox usage efficiently.


Example 2: Add an Email Alias to Multiple Users


Suppose you want to add a new email alias to a list of users stored in a CSV file:


```powershell

$users = Import-Csv -Path "users.csv"


foreach ($user in $users) {

Set-Mailbox -Identity $user.UserPrincipalName -EmailAddresses @{Add="alias@domain.com"}

}

```


This script automates bulk updates, saving time and reducing errors.


Example 3: Create a New Transport Rule to Block Attachments


To block emails with specific attachments:


```powershell

New-TransportRule -Name "Block EXE Attachments" -AttachmentExtensionMatchesWords "exe" -RejectMessageReasonText "Executable attachments are not allowed."

```


This rule helps enforce security policies on incoming mail.


Eye-level view of a computer screen displaying PowerShell commands managing Exchange Online mailboxes
PowerShell ExchangeOnline module commands managing mailboxes

Tips for Using the ExchangeOnline Module


  • Always test scripts in a non-production environment before running them live.


  • Use `Get-Help <cmdlet>` to learn about parameters and examples for each cmdlet.


  • Combine cmdlets with PowerShell scripting features like loops and conditionals for automation.


  • Keep the module updated by running `Update-Module ExchangeOnlineManagement` regularly.


  • Use secure credential handling methods to protect login information.


Summary


The PowerShell ExchangeOnline module offers a powerful way to manage Exchange Online environments. It supports modern authentication and provides a wide range of cmdlets for mailbox management, permissions, distribution groups, and mail flow rules. Using this module can save time, reduce errors, and enable automation for administrators.


 
 
 

Comments


bottom of page