Tuesday, July 23, 2024

Get all Exchange user inclusive details from a list of AD groups

Abstract: You sometimes might need a list of Exchange users which are part from one (or more) AD groups with some additional amount of data from the users Exchange mailboxes.This can be performed via a small powershell script.

The script below will (if not adjusted) collect the users who on your exchange servers started with the name EXMAIL as an *.cvs file with additional information like UserAlias, UserDisplayName, UserItemCount, UserTotalItemSize (in MB), UserLastLogonTime, Database, ServerName, OrganizationalUnit, ActiveSyncStatus, PrimarySmtpAddress. You can import that to Excel (use “|” as separator) and some filter options there if needed.

Write-Host "Please wait ... working ..."

# 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

$Groups = Import-Csv -Delimiter "," -Path ".\GetAllADGroupUsersFromCSVImportedGroupList.csv"


#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 "

foreach ($Group in $Groups)

    $UsernamesInGroup = get-adgroupmember $Group.GroupName -recursive | Select SamAccountName,name

    foreach ($UserNameFromGroup in $UsernamesInGroup)
        # Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,ServerName,PrimarySmtpAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”} | ForEach-Object {$_.SmtpAddress}}}
        $UserMailBox = Get-Mailbox -Identity $UserNameFromGroup.SamAccountName
        # Get the user's alias and display name
        $UserAlias = $UserNameFromGroup.SamAccountName
        $UserDisplayName = $UserNameFromGroup.name
        $UsersMailDB = $UserMailBox.Database
        $UsersServerName = $UserMailBox.ServerName
        if ($UsersServerName -like "EXMAIL*")
            # Get the user's mailbox statistics on ItemCount
            $UserItemCount = Get-MailboxStatistics $UserDisplayName | Select-Object ItemCount
            $UserItemCount = $UserItemCount.ItemCount
            $UserOrgUnit = $UserMailBox.OrganizationalUnit
            # Get PrimarySmtpAddress
            $UserPrimarySmtpAddress = $UserMailBox.PrimarySmtpAddress
            # 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 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

            # 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 from here.

Related Articles


Please enter your comment!
Please enter your name here

Stay Connected

- Advertisement -

Latest Articles