• Health tips to prevent the spread of COVID-19:
    • Wash your hands frequently;
    • Maintain 2 meters social distancing;
    • Minimise outdoor activities.
    Stay safe in your bubble!

List all of Active Mailboxes on Office 365 using PowerShell

Andy

Administrator
User ID
1
Joined
7 Jan 2019
Messages
175
Reaction score
7
Points
18
Preparation:
Windows Remote Management (WinRM) on your computer needs to allow Basic authentication (it's enabled by default). To verify that Basic authentication is enabled, run this command in a Command Prompt:
Code:
winrm get winrm/config/client/auth
If you don't see the value Basic = true, you need to run this command to enable Basic authentication for WinRM:
Code:
winrm set winrm/config/client/auth @{Basic="true"}
Code:
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
Here's the command to list all active users on Office 365:
Code:
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox,SharedMailbox | Get-MailboxStatistics | Sort-Object lastlogontime -Descending | Select-Object DisplayName,LastLogonTime
If you want to filter only to list all active users within the last 30 days:
Code:
Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox,SharedMailbox | Where {(Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-30)} | Sort -Property @{e={( Get-MailboxStatistics $_.Identity).LastLogonTime}} -Descending | Select-Object DisplayName,@{n="LastLogonTime";e={(Get-MailboxStatistics $_.Identity).LastLogonTime}}
If you want to adjust, for example, you want within the last 180 days instead, adjust accordingly. Look closely on the Where filter: (Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-30)

When you've finished, to disconnect your PowerShell session, run the following command:
Code:
Remove-PSSession $Session
If you close the Windows PowerShell window without disconnecting the session, you could use up all the remote PowerShell sessions available to you, and you'll need to wait for the sessions to expire.

Hope this helps. Cheers 🍻
 
 Short URL:
Top