---
title: PowerShell Exchange Online Commands
tags: [powershell, exchange, office365, m365, administration]
---

# PowerShell Exchange Online Commands

This guide provides essential commands for managing Exchange Online using the `ExchangeOnlineManagement` module.

**Parent Topic:** [[programming/PowerShell/PowerShell|PowerShell Comprehensive Guide]]

## 1. Connecting to Exchange Online

First, ensure the module is installed:
```powershell
Install-Module -Name ExchangeOnlineManagement -Scope CurrentUser
```

Then, connect to your tenant. This will prompt for modern authentication (MFA).
```powershell
Connect-ExchangeOnline -UserPrincipalName "admin@yourdomain.com"
```

To disconnect when you are finished:
```powershell
Disconnect-ExchangeOnline
```

## 2. Mailbox Management

### Find Mailboxes
```powershell
# Get a specific mailbox
Get-Mailbox -Identity "user@yourdomain.com"

# Get all user mailboxes
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox

# Get all shared mailboxes
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox
```

### Mailbox Permissions
```powershell
# Grant Full Access permission
Add-MailboxPermission -Identity "shared.mailbox@yourdomain.com" -User "user@yourdomain.com" -AccessRights FullAccess -InheritanceType All

# Grant Send As permission
Add-RecipientPermission -Identity "shared.mailbox@yourdomain.com" -Trustee "user@yourdomain.com" -AccessRights SendAs
```

### Mailbox Forwarding
```powershell
# Set up email forwarding and keep a copy in the original mailbox
Set-Mailbox -Identity "user.a@yourdomain.com" -DeliverToMailboxAndForward $true -ForwardingSmtpAddress "user.b@yourdomain.com"

# Remove forwarding
Set-Mailbox -Identity "user.a@yourdomain.com" -ForwardingSmtpAddress $null
```

## 3. Mail Flow & Transport Rules
```powershell
# Get all transport rules
Get-TransportRule

# Create a rule to prepend a subject for external emails
New-TransportRule -Name "External Mail Warning" -FromScope NotInOrganization -ApplyHtmlDisclaimerLocation Prepend -ApplyHtmlDisclaimerText "<p><div style='background-color:#FFEB9C; padding:5px; border:1px solid #7D6608;'><strong>CAUTION:</strong> This email originated from outside of the organization.</div></p>"
```

---
Return to [[programming/PowerShell/PowerShell|PowerShell Comprehensive Guide]]