Office 365 LastLogonTime report. Find inactive users on Office 365.

Took me a while to find this code on another site.. I’ve modified it slightly.

This reports the last log on date of every user in your organisation.

$objUsers = get-mailbox -ResultSize Unlimited | select UserPrincipalName 

Foreach ($objUser in $objUsers) 
    {     
        #Connect to the users mailbox 
        $objUserMailbox = get-mailboxstatistics -Identity $($objUser.UserPrincipalName) | Select LastLogonTime 
         
        #Prepare UserPrincipalName variable 
        $strUserPrincipalName = $objUser.UserPrincipalName 
         
        #Check if they have a last logon time. Users who have never logged in do not have this property 
        if ($objUserMailbox.LastLogonTime -eq $null) 
        { 
            #Never logged in, update Last Logon Variable 
            $strLastLogonTime = "Never Logged In" 
        } 
        else 
        { 
            #Update last logon variable with data from Office 365 
            $strLastLogonTime = $objUserMailbox.LastLogonTime 
        } 
         
        #Output result to screen for debuging (Uncomment to use) 
        write-host "$strUserPrincipalName : $strLastLogonTime"
    }

Copy and paste it into the PowerShell after connecting and then press enter twice.. Should show you a list of users and their last sign in time.

Hope this helps someone.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Office 365 LastLogonTime report. Find inactive users on Office 365.

  1. Jimmy says:

    What if the user does not have a mailbox? Perhaps I’ve provisioned a user and only want them to use SharePoint online so I don’t give them Exchange, is there an option for finding out last logon?

Comment on this topic

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s