I have prepared two PS scripts for you to get last logged in details, one for to get mailusers details and other for mailcontacts details. The csv results can help to decide on your cleanup
First of all connect to the Office 365 tenant
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Run the script one by one (eg: .\mailuser.ps1). Make sure that you have a folder created as temp under C:\
- Mail Contacts $Result=@() $mailboxes = Get-MailContact -ResultSize Unlimited $totalmbx = $mailboxes.Count $i = 1 $mailboxes | ForEach-Object { $i++ $mbx = $_ $mbs = Get-MailContact -Identity $mbx.UserPrincipalName | Select LastLogonTime if ($mbs.LastLogonTime -eq $null){ $lt = "Never Logged In" }else{ $lt = $mbs.LastLogonTime } $Result += New-Object PSObject -property @{ Name = $mbx.DisplayName UserPrincipalName = $mbx.UserPrincipalName LastLogonTime = $lt } } $Result | Export-CSV "C:\temp\LastLogon-details-contacts.csv" -NoTypeInformation -Encoding UTF8
- Mail Users $Result=@()
$mailboxes = Get-MailUser -ResultSize Unlimited
$totalmbx = $mailboxes.Count
$i = 1
$mailboxes | ForEach-Object {
$i++
$mbx = $_
$mbs = Get-MailUser -Identity $mbx.UserPrincipalName | Select LastLogonTime
if ($mbs.LastLogonTime -eq $null){
$lt = "Never Logged In"
}else{
$lt = $mbs.LastLogonTime }
$Result += New-Object PSObject -property @{
Name = $mbx.DisplayName
UserPrincipalName = $mbx.UserPrincipalName
LastLogonTime = $lt }
}
$Result | Export-CSV "C:\temp\LastLogon-details.csv" -NoTypeInformation -Encoding UTF8
Results are expected to be saved under C:\temp