Share via


Exchange 2010/2013/2016: Get last logon time for mailbox in an OU

In exchange, many commands are directed at all mailboxes in the environment, but there is a time when you only want info on a specific organizational unit (OU).

It is actually very easy to do, below is a snippet of a script you can use to get the last logon information for an OU which also lists things like the Mailbox Size, Last Logon Date etc.

$users = get-mailbox -organizationalunit "attribute here" -resultsize unlimited

$userarray = @()

foreach ($user in $users)

{

$MailUser = $user.UserPrincipalName

$stats= Get-MailboxStatistics $MailUser

$datetime = $stats.LastLogonTime

$date, $time = $datetime -split(' ')

$Maildetails = New-Object -TypeName PSObject -Property @{

DisplayName = $stats.DisplayName

ItemCount = $stats.ItemCount

MailboxSize = $stats.TotalItemSize

LastLogonDate = $date

LastLogonTime = $time

Email = $MailUser

 

}

 

$userarray += $Maildetails

}

 

$userarray | Export-Csv -Path C:\Users1.csv

It outputs to a CSV file, I saved the data as and Excel workbook and this is what it looks like:

You can head over to the gallery here and grab the PS1 script.

In the script on line 1 in inverted commas "", put the attribute of the OU in and on the last line, specify where you want the csv file to export to.

https://gallery.technet.microsoft.com/Get-last-logon-time-for-11e894f5