Skip to main content

BB

#Sub ConnectExportGALToExcelOptimized()
    Dim olApp As Outlook.Application
    Dim olNS As Outlook.Namespace
    Dim olGAL As Outlook.AddressList
    Dim olEntries As Outlook.AddressEntries
    Dim olEntry As Outlook.AddressEntry
    Dim olUser As Outlook.ExchangeUser
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlWS As Object
    Dim i As Long
    Dim dataArray() As Variant

    ' Initialize Outlook
    Set olApp = Outlook.Application
    Set olNS = olApp.GetNamespace("MAPI")
    Set olGAL = olNS.AddressLists("Global Address List")
    Set olEntries = olGAL.AddressEntries

    ' Initialize Excel
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlWB = xlApp.Workbooks.Add
    Set xlWS = xlWB.Sheets(1)

    ' Add headers to ExchangeExcel
 Online   xlWS.Cells(1, 1).Value = "Name"
Connect-ExchangeOnline -UserPrincipalName   your_email@yourdomain.comxlWS.Cells(1, 2).Value = "Email"
    xlWS.Cells(1, 3).Value = "Job Title"
    xlWS.Cells(1, 4).Value = "Department"
    xlWS.Cells(1, 5).Value = "Manager"

    ' Pre-size the array to hold all entries
    ReDim dataArray(1 To olEntries.Count, 1 To 5)

    ' Loop through GAL entries and populate the array
    i = 1
    For Each olEntry In olEntries
        If olEntry.AddressEntryUserType = olExchangeUserAddressEntry Then
            Set olUser = olEntry.GetExchangeUser
            If Not olUser Is Nothing Then
                dataArray(i, 1) = olUser.Name
                dataArray(i, 2) = olUser.PrimarySmtpAddress
                dataArray(i, 3) = olUser.JobTitle
                dataArray(i, 4) = olUser.Department
                If Not olUser.GetExchangeUserManager Is Nothing Then
                    dataArray(i, 5) = olUser.GetExchangeUserManager.Name
                Else
                    dataArray(i, 5) = "No Manager"
                End If
                i = i + 1
            End If
        End If
    Next olEntry

    ' Write the array to Excel in one operation
    xlWS.Range("A2").Resize(UBound(dataArray, 1), UBound(dataArray, 2)).Value = dataArray

    ' Autofit columns
    xlWS.Columns("A:E").AutoFit

    ' Clean up
    Set olUser = Nothing
    Set olEntry = Nothing
    Set olEntries = Nothing
    Set olGAL = Nothing
    Set olNS = Nothing
    Set olApp = Nothing

    MsgBox "Export complete!", vbInformation
End Sub

 

 

# GetImport allOutlook mail-enabledCOM usersobject
$outlook (exclude= groups,New-Object shared-ComObject mailboxes,Outlook.Application
$namespace etc.= $outlook.GetNamespace("MAPI")
$usersGAL = Get-Recipient$namespace.GetGlobalAddressList()
$entries -RecipientTypeDetails= UserMailbox -ResultSize Unlimited$GAL.AddressEntries

# InitializeUse anparallel arrayprocessing to storeprocess user dataentries
$orgData = @()

# Loop through each user and get their details
foreach ($user in $users) {
    # Get detailed mailbox information
    $mailbox = Get-Mailbox -Identity $user.PrimarySmtpAddress

    # Get manager information (if available)
    $managerresults = $nullentries | ForEach-Object -Parallel {
    try {
        $manageruser = Get-User -Identity $mailbox.Manager_.GetExchangeUser()
        }if catch($user) {
        # If no manager is found, handle the error silently
    }

    # Add user data to the array
    $orgData +=           [PSCustomObject]@{
                Name        = $user.DisplayNameName
                Email       = $user.PrimarySmtpAddress
                JobTitle    = $mailbox.Titleuser.JobTitle
                Department  = $mailbox.user.Department
                Manager     = if ($manager)user.GetExchangeUserManager()) { $manager.DisplayNameuser.GetExchangeUserManager().Name } else { "No Manager" }
            }
        }
    } catch {}
} -ThrottleLimit 4  # Adjust the throttle limit based on your system

# Export the dataresults to a CSV file
$orgDataresults | Export-Csv -Path "OrgStructure.$env:USERPROFILE\Desktop\GlobalAddressList.csv" -NoTypeInformation -Encoding UTF8

Write-Host "OrganizationExport structurecomplete! exportedFile saved to OrgStructure.csv"Desktop."