Get-GALFromOutlook.ps1

<#PSScriptInfo
 
.VERSION 1.2
 
.GUID 3a586aff-8fa4-4766-b669-7b0a1ccb554a
 
.AUTHOR Aaron Guilmette
 
.COMPANYNAME Microsoft
 
.COPYRIGHT 2020
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI https://www.undocumented-features.com/2019/02/12/export-user-gal-entries-from-outlook/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.DESCRIPTION
Export GAL from Outlook session.
 
.PRIVATEDATA
 
#>


<#
.SYNOPSIS
Export GAL from Outlook session.
 
.PARAMETER OutFile
Specify output file for GAL export.
 
.LINK
https://www.undocumented-features.com/2019/02/12/export-user-gal-entries-from-outlook/
 
.NOTES
2019-05-10 - Updated with additional GAL entry fields.
           - Mapped 'PrimarySmtpAddress' to 'UserPrincipalName' so you can more
               easily use the tool with http://aka.ms/pwncheck
2019-02-13 - Update progress bar to show current/total objects.
2019-02-12 - Initial release.
#>

param (
$OutFile = (Get-Date -Format yyyy-MM-dd) + "_GALEntries.csv"
)

$Outlook = New-Object -ComObject Outlook.Application
$GlobalAddressList = $Outlook.Session.GetGlobalAddressList().AddressEntries
$TotalObjects = $GlobalAddressList.Count

$i = 1
foreach ($entry in $GlobalAddressList)
{
    Write-Progress -Id 1 -Activity "Exporting Global Address List Entries" -PercentComplete (($i / $TotalObjects) * 100) -Status "[$($i)/$($TotalObjects)] entries exported"
    If ($entry.Address -match "\/o\=")
    {
        $EntryData = $entry.GetExchangeUser()
        $RecordData = [ordered]@{
            Name                = $EntryData.Name
            First                = $EntryData.FirstName
            Last                = $EntryData.Last
            PrimarySmtpAddress     = $EntryData.PrimarySmtpAddress
            UserPrincipalName    = $EntryData.PrimarySmtpAddress
            x500                 = $EntryData.Address
            Alias                = $EntryData.Alias
            AssistantName         = $EntryData.AssistantName
            BusinessPhone         = $EntryData.BusinessTelephoneNumber
            MobilePhone            = $EntryData.MobileTelephoneNumber
            Title                 = $EntryData.JobTitle
            Department            = $EntryData.Department
            Company              = $EntryData.CompanyName
            OfficeLocation         = $EntryData.OfficeLocation
            Address             = $EntryData.StreetAddress
            City                = $EntryData.City
            StateOrProvince     = $EntryData.StateOrProvince
            PostalCode            = $EntryData.PostalCode
        }
        $Record = New-Object PSobject -Property $RecordData
        $Record | Export-csv $OutFile -NoTypeInformation -Append
    }
    $i++
}
Write-Progress -Id 1 -Status "Completed." -Completed