How to get email address and lastlogontime

Hi Everyone,

Hoping I can get some assistance with something I am stuck on.

Periodically I need to pull a report of inactive users so that I can then disable or delete accounts to free up Office 365 licenses. I am using as follows…

Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | select Displayname, lastlogontime | Export-Csv C:\inactive.csv

When I do this, results come in fine, I get name and date no problem. But if I try to either add a user’s SMTP address or just try to use this…

Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | select PrimarySMTPAddress, lastlogontime | Export-Csv C:\inactive.csv

Primarysmtpaddress comes back blank. And that’s using Export-CSV or Format-Table.

I know it’s an intensive search and I also know Get-MailboxStatistics isn’t very efficient. But my question is how can I reliably get this sort of report from O365 using Powershell?

Thanks in advance!

Nelson

 

I cannot test at the moment, but if I’m not wrong Get-MailboxStatistics does not output a property PrimarySMTPAddress. That should be a property of the output of Get-Mailbox. Therefore you cannot simply use Select-Object in this case …
Something like this should work actually

Get-Mailbox -resultsize unlimited |
ForEach-Object {
[PSCustomObject]@{
PrimarySMTPAddress = $.PrimarySMTPAddress
lastlogontime = (Get-MailboxStatistics $
).lastlogontime
}
} |
Export-Csv C:\inactive.csv

Thanks. You’re right about that. I’m a newbie still and didn’t realize what I was passing to the pipeline. I did a gm on Get-MailboxStatistics to confirm.

I’ll try what you posted, should work for me. Thanks so much!

Nelson

As for you use case…

Periodically I need to pull a report of inactive users so that I can then disable or delete accounts to free up Office 365 licenses.

… why reinvent the wheel, when MS provide direct guidance on how to handle this.

There is a specific cmdlet for finding user state, depending on what OS/PS version you are running, the help files show:

# get function / cmdlet details
(Get-Command -Name Search-ADAccount).Parameters.Keys | Sort
Get-help -Name Search-ADAccount -Full
Get-help -Name Search-ADAccount -Online
Get-help -Name Search-ADAccount -Examples

Function Get-HelpExamples
{
    [CmdletBinding()]
    [Alias('ghe')]

    Param
    (
        [string]$CmdletName = (
            Get-Command -Name '*' | 
            Out-GridView -PassThru -Title 'Select a cmdlet to see examples'
        )
    )

    If ((Get-Help -Name $CmdletName).Examples)
    {
        (((Get-Help -Name $CmdletName).Examples | 
        Out-String -Stream) -match '.*\\>|C:\\PS>') -replace '.*\\>|C:\\PS>' | 
        Out-GridView -Title 'Select a sample to use' -PassThru
    }
    Else {Write-Warning -Message "The were no help examples discovered"}
}

ghe -CmdletName Search-ADAccount

# Results

Search-ADAccount -AccountDisabled | FT Name,ObjectClass -A
Search-ADAccount -AccountDisabled -UsersOnly | FT Name,ObjectClass -A
Search-ADAccount -AccountExpired | FT Name,ObjectClass -A
Search-ADAccount -AccountExpiring -TimeSpan 6.00:00:00 | FT Name,ObjectClass -A
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 | FT Name,ObjectClass -A
Search-ADAccount -PasswordExpired | FT Name,ObjectClass -A
Search-ADAccount -PasswordNeverExpires | FT Name,ObjectClass -A
Search-ADAccount -LockedOut | FT Name,ObjectClass -A
Search-ADAccount -AccountDisabled -ComputersOnly | FT Name,ObjectClass -A
Search-ADAccount -AccountExpiring -DateTime "3/18/2009" | FT Name,ObjectClass -A
Search-AdAccount -AccountDisabled -SearchBase "DC=AppNC" -Server "FABRIKAM-SRV1:60000"

Create and manage inactive mailboxes in Office 365

Remove O365 Licenses from Disabled Active Directory Users
This Script will remove Office 365 licenses from synchronized users that have been blocked within Office 365 because they are disabled in Active Directory. Azure AD Connect doesn’t remove an O365 license when a user’s Active Directory account gets disabled.

Download : Remove-office365Licenses.ps1

Using PowerShell to Find Disabled or Inactive User Accounts in Active Directory

Remove O365 Licenses from Disabled Active Directory Users
This Script will remove Office 365 licenses from synchronized users that have been blocked within Office 365 because they are disabled in Active Directory. Azure AD Connect doesn’t remove an O365 license when a user’s Active Directory account gets disabled.

Download : Remove-office365Licenses.ps1

See also:
Find Inactive Users in Office 365
How to Disable Inactive User Accounts Using PowerShell

Thanks, this is really helpful. Appreciate it!

Nelson