Link to home
Start Free TrialLog in
Avatar of raju1706
raju1706Flag for United States of America

asked on

Mailbox Statistics Exchange 2007

In need a powershell query which can get the following details for users lastlogon > 60days

First Name,Last Name,Display Name,Alias, Name, Server, City, Database, Country/Region, Department,Hidden From Address Lists,Office,Phone,Postal Code,Primary SMTP Address,State Or Province,Storage Group,Recipient Type Details

I think Get-Mailbox, Get-Mailboxstastics and Get-User cmdelts have to be used.

Thanks in Advance

Avatar of Sekar Chinnakannu
Sekar Chinnakannu
Flag of Singapore image

#        Name:  get-lastlogon.ps1
#      Author:  Eric Woodford - <a href="
#" title="
#</a>        Date:  09/26/2007
#        Description:  Mailbox last logon information to single CSV file.
#              Display Name, Last Logon Time

#create header for CSV file.
$CSVFilePath = 'c:\mail_LastLogon.csv'
$strDate = get-date -uformat "%Y/%m/%d"
$strDate | out-file -filepath $CSVFilePath -encoding ascii
$strOutputString = "Display Name,LastLogonTime"
$strOutputString | out-file -filepath $CSVFilePath -encoding ascii -append

#set this to match your environment.
$Computers = get-qadComputer -searchRoot 'corp.ent/Member Servers/Exchange Servers'
foreach ($computer in $computers) {
        $users = Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $ | Select-Object MailBoxDisplayName, LastLogonTime
        # Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $ | Select-Object MailBoxDisplayName, LastLogonTime
        foreach ($user in $users) {
                   $date= [string] $user.LastLogonTime
                   if ($date.length -eq 0) {
                        $strOutputString = """"+ $user.MailBoxDisplayName + """, N/A"                          
                else {
                        $strOutputString = """"+ $user.MailBoxDisplayName + """," + $date.substring(4,2)+"/"+$date.substring(6,2) +"/"+ $date.substring(0,4)
           $strOutputString | out-file -filepath $CSVFilePath -encoding ascii -append
Avatar of raju1706


@sekarc4u Thanks for the response. I cannot use Quest cmdlets.
Get-MailboxStatistics isn't too much help because it does not only show user logins on the mailbox. It shows any account that logs in, including system accounts.

Still, it can be done, it's just the first part is quite complex. I assume you want this out to a file?
# Filter to get users who have logged in within the last 60 days
$Date = (Get-Date).AddDays(-60).Date.ToFileTime()
$LdapFilter = "(&(objectClass=user)(objectCategory=person)(lastLogonTimeStamp>=$Date)(mail=*))"

$Searcher = New-Object DirectoryServices.DirectorySearcher($LdapFilter)
$Searcher.PageSize = 1000

$Searcher.FindAll() | ForEach-Object { $_.Properties["distinguishedname"][0] } | Get-User | ForEach-Object {
  $Mailbox = $_ | Get-Mailbox

  $_ | Select-Object FirstName, LastName, DisplayName, @{n='Alias';e={ $Mailbox.Alias }}, Name,
    @{n='Server';e={ $Mailbox.ServerName }}, City, @{n='Database';e={ $Mailbox.Database }}, 
    CountryOrRegion, Department, @{n='HiddenFromAddressLists';e={ $Mailbox.HiddenFromAddressListsEnabled }},
    Office, Phone, PostalCode, WindowsEmailAddress, StateOrProvince, RecipientTypeDetails
} | Export-Csv "YourOutput.csv" -NoTypeInformation

Open in new window

I have not exported StorageGroup, it's included in the Database field.


I am a beginner in powershell , if you could explain the script it would be  great.

Secondly can you also add Storage Limit Status to the script?

Thank You!!
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial