Abstract: You sometimes might need a list of users who didn´t have logged in since a some days (e.g. Exchange cleanup, to prepare a migration, ...) with an additional amount of data. This can be performed via a small powershell script.

The script below will (if not adjusted) collect Microsoft Exchange users who haven´t been logged in on Exchange since 7 days inside an *.cvs file with additional information like UserAlias, UserDisplayName, UserItemCount, UserTotalItemSize (in MB), UserLastLogonTime, Database, ServerName, OrganizationalUnit, ActiveSyncStatus, PrimarySmtpAddress

Write-Host "Please wait ... working ..."
Write-Host "Building List of users who didn´t logged in since 7 days on a server started with the name"

# If you need another time value adjust the part AddDays(-7)
# Keep noted that the script is written for an Exchange cluster with two nodes called exmail01 and exmail02. If your is different adjust the -like "EXMAIL*" part

$Users = Get-Mailbox -OrganizationalUnit "DC=int,DC=contoso,DC=com" -ResultSize unlimited


#Build header from export:
$OutputHeaderString = "UserAlias|UserDisplayName|UserItemCount|UserTotalItemSize (in MB)|UserLastLogonTime|Database|ServerName|OrganizationalUnit|ActiveSyncStatus|PrimarySmtpAddress"
$OutputHeaderString | Out-File $OutPutFile1 -Append
Write-Host "UserAlias|UserDisplayName|UserItemCount|UserTotalItemSize (in MB)|UserLastLogonTime|Database|ServerName|OrganizationalUnit|ActiveSyncStatus|PrimarySmtpAddress"
# Do the following for each user
foreach ($User in $Users)
    # Get the user's alias and display name
    $UserAlias = $User.Alias
    $UserDisplayName = $User.DisplayName
    $UsersMailDB = $User.Database
    $UsersServerName = $User.ServerName

    # Get the user's mailbox statistics on ItemCount
    $UserItemCount = Get-MailboxStatistics $UserDisplayName | Select-Object ItemCount
    $UserItemCount = $UserItemCount.ItemCount
    $UserOrgUnit = $User.OrganizationalUnit
    # Get the users active sync status
    # Get-CASMailbox -Identity wieczorek.5 |select ActiveSyncEnabled
    $UserActiveSyncStatusTEMP = Get-CASMailbox -Identity $UserDisplayName | Select-Object ActiveSyncEnabled
    $UserActiveSyncStatus = $UserActiveSyncStatusTEMP.ActiveSyncEnabled
    # Get PrimarySmtpAddress
    $UserPrimarySmtpAddress = $User.PrimarySmtpAddress
    # Get the user's mailbox statistics on TotalItemSize, format to just MB / GB
    $UserTotalItemSize = Get-MailboxStatistics $UserDisplayName | Select-Object TotalItemSize
    $UserTotalItemSize = $UserTotalItemSize.TotalItemSize.Value.toMB()
    #$UserTotalItemSize = $UserTotalItemSize.TotalItemSize.Value.toGB()
    # Get the user's mailbox statistics on LastLogonTime
    $UserLastLogonTime = Get-MailboxStatistics $UserDisplayName | Select-Object LastLogonTime
    $UserLastLogonTime = $UserLastLogonTime.LastLogonTime
    # Only users who didn´t logged into the environment in the past x days
    if ($UserLastLogonTime -lt (get-date).AddDays(-7))
        # Combine results into a pipe-delimited string
        $OutputString = $UserAlias + "|" + $UserDisplayName + "|" + $UserItemCount + "|" + $UserTotalItemSize + "|" + $UserLastLogonTime + "|" + $UsersMailDB + "|" + $UsersServerName + "|" + $UserOrgUnit + "|" + $UserActiveSyncStatus + "|" + $UserPrimarySmtpAddress
        # Write the results to a .csv file
        $OutputString | Out-File $OutPutFile1 -Append
        # Display the results on the screen to show progress
        Write-Host $OutputString

You can download the script here.

Leave your comments

Post comment as a guest



    • No comments found