{"id":3130,"date":"2024-09-30T11:05:04","date_gmt":"2024-09-30T05:35:04","guid":{"rendered":"https:\/\/www.nettigritty.com\/kb\/?p=3130"},"modified":"2024-09-30T11:05:47","modified_gmt":"2024-09-30T05:35:47","slug":"how-to-connect-to-microsoft-365-using-powershell","status":"publish","type":"post","link":"https:\/\/www.nettigritty.com\/kb\/microsoft-365\/how-to-connect-to-microsoft-365-using-powershell\/","title":{"rendered":"How to connect to Microsoft 365 using PowerShell"},"content":{"rendered":"<p>Here\u2019s a guide on how to connect to Microsoft 365 using PowerShell (also referred to as Exchange Online PowerShell) and perform some\u00a0 tasks, such as checking the status of a migration, getting mailbox folder statistics, disabling Single Item Recovery, and changing the &#8220;RetainDeletedItemsFor&#8221; setting.<\/p>\n<h3>Step 1: Connect to Microsoft 365 using PowerShell<\/h3>\n<p>Before running commands in Exchange Online, you need to connect to the service using PowerShell.<\/p>\n<h4>1. Install the Exchange Online Management Module<\/h4>\n<p>If you haven\u2019t installed the module yet, run this in PowerShell:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Install-Module -Name ExchangeOnlineManagement <\/code><\/div>\n<\/div>\n<p>You might need to allow the installation of untrusted modules by using:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Set-ExecutionPolicy RemoteSigned <\/code><\/div>\n<\/div>\n<h4>2. Import the module and connect to Exchange Online<\/h4>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Import-Module ExchangeOnlineManagement <\/code><\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Connect-ExchangeOnline -UserPrincipalName &lt;YourEmail@domain.com&gt; <\/code><\/div>\n<\/div>\n<p>This will prompt you for your credentials. Once authenticated, you\u2019ll be connected to Exchange Online.<\/p>\n<hr \/>\n<h3>Step 2: Check the Status of a Migration<\/h3>\n<p>To check the status of a migration (for example, if you are migrating mailboxes from on-premises to the cloud), use the following command:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Get-MigrationUser <\/code><\/div>\n<\/div>\n<p>This command will display the status of all the users in migration batches.<\/p>\n<p>To get details for a specific user or a batch, you can run:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Get-MigrationUser -Identity &lt;user-email&gt; <\/code><\/div>\n<\/div>\n<p>To check migration batches and their statuses:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Get-MigrationBatch <\/code><\/div>\n<\/div>\n<p>This will display information about active migration batches.<\/p>\n<hr \/>\n<h3>Step 3: Get-MailboxFolderStatistics<\/h3>\n<p>You can retrieve statistics about the folders within a mailbox using the following command:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Get-MailboxFolderStatistics -Identity &lt;user-email&gt; <\/code><\/div>\n<\/div>\n<p>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:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Get-MailboxFolderStatistics -Identity &lt;user-email&gt; | Where-Object {$_.FolderPath -eq \"\/Inbox\"} <\/code><\/div>\n<\/div>\n<hr \/>\n<h3>Step 4: Disable Single Item Recovery (SingleItemRecoveryEnabled)<\/h3>\n<p>To disable Single Item Recovery for a specific mailbox (which allows you to recover deleted items even after they\u2019ve been purged from the Deleted Items folder):<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Set-Mailbox -Identity &lt;user-email&gt; -SingleItemRecoveryEnabled $false <\/code><\/div>\n<\/div>\n<p>You can verify whether Single Item Recovery is enabled or disabled for a mailbox with this command:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Get-Mailbox -Identity &lt;user-email&gt; | Select-Object SingleItemRecoveryEnabled <\/code><\/div>\n<\/div>\n<hr \/>\n<h3>Step 5: Change the RetainDeletedItemsFor Setting<\/h3>\n<p>The <code>RetainDeletedItemsFor<\/code> setting controls how long deleted items are kept in the Recoverable Items folder. You can change this value (in days) using:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Set-Mailbox -Identity &lt;user-email&gt; -RetainDeletedItemsFor &lt;number-of-days&gt; <\/code><\/div>\n<\/div>\n<p>For example, to set the retention period to 30 days:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Set-Mailbox -Identity &lt;user-email&gt; -RetainDeletedItemsFor 30 <\/code><\/div>\n<\/div>\n<p>You can check the current setting with:<\/p>\n<div class=\"dark bg-gray-950 contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-powershell\">Get-Mailbox -Identity &lt;user-email&gt; | Select-Object RetainDeletedItemsFor <\/code><\/div>\n<\/div>\n<hr \/>\n<h3>Summary of Key Commands:<\/h3>\n<ol>\n<li><strong>Connect to Microsoft 365<\/strong>:\n<ul>\n<li><code>Connect-ExchangeOnline -UserPrincipalName &lt;YourEmail@domain.com&gt;<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>Check the status of a migration<\/strong>:\n<ul>\n<li><code>Get-MigrationUser<\/code><\/li>\n<li><code>Get-MigrationBatch<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>Get Mailbox Folder Statistics<\/strong>:\n<ul>\n<li><code>Get-MailboxFolderStatistics -Identity &lt;user-email&gt;<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>Disable Single Item Recovery<\/strong>:\n<ul>\n<li><code>Set-Mailbox -Identity &lt;user-email&gt; -SingleItemRecoveryEnabled $false<\/code><\/li>\n<\/ul>\n<\/li>\n<li><strong>Change RetainDeletedItemsFor<\/strong>:\n<ul>\n<li><code>Set-Mailbox -Identity &lt;user-email&gt; -RetainDeletedItemsFor 30<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>This guide should help you accomplish the tasks you outlined efficiently. Let me know if you need further clarification or additional details!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here\u2019s a guide on how to connect to Microsoft 365 using PowerShell (also referred to as Exchange Online PowerShell) and perform some\u00a0 tasks, such as checking the status of a migration, getting mailbox folder statistics, disabling Single Item Recovery, and changing the &#8220;RetainDeletedItemsFor&#8221; setting. Step 1: Connect to Microsoft 365 using PowerShell Before running commands [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[109],"tags":[],"class_list":["post-3130","post","type-post","status-publish","format-standard","hentry","category-microsoft-365"],"_links":{"self":[{"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/posts\/3130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/comments?post=3130"}],"version-history":[{"count":2,"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/posts\/3130\/revisions"}],"predecessor-version":[{"id":3132,"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/posts\/3130\/revisions\/3132"}],"wp:attachment":[{"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/media?parent=3130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/categories?post=3130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nettigritty.com\/kb\/wp-json\/wp\/v2\/tags?post=3130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}