How to connect to Microsoft 365 using PowerShell

Here’s a guide on how to connect to Microsoft 365 using PowerShell (also referred to as Exchange Online PowerShell) and perform some  tasks, such as checking the status of a migration, getting mailbox folder statistics, disabling Single Item Recovery, and changing the “RetainDeletedItemsFor” setting.

Step 1: Connect to Microsoft 365 using PowerShell

Before running commands in Exchange Online, you need to connect to the service using PowerShell.

1. Install the Exchange Online Management Module

If you haven’t installed the module yet, run this in PowerShell:

Install-Module -Name ExchangeOnlineManagement

You might need to allow the installation of untrusted modules by using:

Set-ExecutionPolicy RemoteSigned

2. Import the module and connect to Exchange Online

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName <YourEmail@domain.com>

This will prompt you for your credentials. Once authenticated, you’ll be connected to Exchange Online.


Step 2: Check the Status of a Migration

To check the status of a migration (for example, if you are migrating mailboxes from on-premises to the cloud), use the following command:

Get-MigrationUser

This command will display the status of all the users in migration batches.

To get details for a specific user or a batch, you can run:

Get-MigrationUser -Identity <user-email>

To check migration batches and their statuses:

Get-MigrationBatch

This will display information about active migration batches.


Step 3: Get-MailboxFolderStatistics

You can retrieve statistics about the folders within a mailbox using the following command:

Get-MailboxFolderStatistics -Identity <user-email>

This will display information such as folder size, item count, and other details for each folder in the mailbox. To get more detailed information on a specific folder (like the Inbox), you can use:

Get-MailboxFolderStatistics -Identity <user-email> | Where-Object {$_.FolderPath -eq "/Inbox"}

Step 4: Disable Single Item Recovery (SingleItemRecoveryEnabled)

To disable Single Item Recovery for a specific mailbox (which allows you to recover deleted items even after they’ve been purged from the Deleted Items folder):

Set-Mailbox -Identity <user-email> -SingleItemRecoveryEnabled $false

You can verify whether Single Item Recovery is enabled or disabled for a mailbox with this command:

Get-Mailbox -Identity <user-email> | Select-Object SingleItemRecoveryEnabled

Step 5: Change the RetainDeletedItemsFor Setting

The RetainDeletedItemsFor setting controls how long deleted items are kept in the Recoverable Items folder. You can change this value (in days) using:

Set-Mailbox -Identity <user-email> -RetainDeletedItemsFor <number-of-days>

For example, to set the retention period to 30 days:

Set-Mailbox -Identity <user-email> -RetainDeletedItemsFor 30

You can check the current setting with:

Get-Mailbox -Identity <user-email> | Select-Object RetainDeletedItemsFor

Summary of Key Commands:

  1. Connect to Microsoft 365:
    • Connect-ExchangeOnline -UserPrincipalName <YourEmail@domain.com>
  2. Check the status of a migration:
    • Get-MigrationUser
    • Get-MigrationBatch
  3. Get Mailbox Folder Statistics:
    • Get-MailboxFolderStatistics -Identity <user-email>
  4. Disable Single Item Recovery:
    • Set-Mailbox -Identity <user-email> -SingleItemRecoveryEnabled $false
  5. Change RetainDeletedItemsFor:
    • Set-Mailbox -Identity <user-email> -RetainDeletedItemsFor 30

This guide should help you accomplish the tasks you outlined efficiently. Let me know if you need further clarification or additional details!

Was this article helpful?
YesNo