User Mailbox Inventory

Hey,

The following code allowed me to obtain what i needed but i wondered if there was a better way to structure my query? Basically i am looking to pull out the LastLogonTime for mailbox users on Exchange as a means of identifying stale accounts, the below gives me what i need albeit i manipulate some of the rows in the spreadsheet after (to show nothing older than Feb 2020).

Get-MailboxStatistics -Server Exchange2010 | Sort LastLogonTime -Descending | Select DisplayName,LastLogonTime | Export-CSV C:\Test.csv

You can use a Where-Object filter to limit the output of what you’re after:

$Limit = Get-Date ‘1/2/2020’
Get-MailboxStatistics -Server Exchange2010 |
Where-Object {(Get-Date $_.LastLogonTime) -lt $Limit} |
Sort-Object -Property LastLogonTime -Descending |
Select-Object -Property DisplayName,LastLogonTime |
Export-CSV -Path C:\Test.csv -NoTypeInformation

Depending on your locale setting is the first of February either ‘2/1/2020’ or ‘1/2/2020’. :wink:

Hey Olaf

Thanks for the Feedback. The code makes sense, however it returns all results and not just those post 01/02/20 - same output as the command i used in effect. I tried with gt, le and ge but these came up blank.

Thanks

Tony

As Olaf stated, (my kids and us love you btw,) where-object can filter the output. I’ve captured the cutoff date in a variable after casting to datetime. I tested with both 2020-02-01 and 02-01-2020. You should be able to use the -filter parameter on Get-MailboxStatistics to optimize the speed. With the Where-Object filtering, all output is gathered and then the filter is applied. I did not figure out the filter with the date yet, if I do i’ll come post it.

$cutoffdate = [datetime]"02-01-2020";
Get-MailboxStatistics -Server Exchange2010 |
    where lastlogontime -gt $cutoffdate |
        select DisplayName,LastLogonTime,LastLogoffTime,ItemCount,TotalItemSize,DatabaseName |
            Export-CSV C:\Temp\Test.csv -NoTypeInformation

I’ve grabbed a few other properties that may be useful. I really hope this helps.

Thanks Doug, doesnt work for me im afraid - cannot bind LastLogonTime to the $cutoffdate variable (presumably this is because the variable is DateTime and LastLogonTime is string so cant match).

Appreciate the input from you guys here.

If you’re copying/pasting the code, beware that it combines the first line with the rest. I’ve added a semicolon at the end of it for others that may try to copy/paste. I also changed the path to C:\Temp\ as even with the exchange shell “ran as admin” it denied write access to C for me. Let me know if this works now. It works fine on my exchange server.

If that’s the case you can convert the string into a DateTime object with Get-Date. … I changed the code in my answer above … try it now. :wink:

Guys thanks for the feedback here. I have tried both methods and neither seem to be working right for me.

Im due to patch this Exchange server tonight, its way out of date so potentially that is resulting in this bugging out with your commands if they are testing fine for you.

How are you running it? If you’re pasting it into powershell I’ve seen it go in out of order. If you haven’t, try opening ISE and pasting it into a blank script. Then highlight and run selection. You could also try saving it as a PS1 file and running it that way. Lastly, you can run the commands one at a time to see what isn’t working. Does get-mailboxstatistics work by itself? Maybe the command isn’t available in the powershell session you’re attempting it in?

I do not have access to an Exchange environment to test at the moment. You may debug these few lines yourself.