Export GAL Users and Contacts

This Script creates a CSV Log file of all Users and Contact in a specified OU.

The Log file can be used to import contacts into Outlook.


Download The Script

 


# Author : Assaf Miron

# Http://assaf.miron.googlepages.com

# Description :

# This Script creates a CSV Log file of all Users and Contact in a specified OU.

# This Log file can be used to import contacts into Outlook.

# Input : 

# Output: A CSV with all the Mail Users and Mail Contacts in spcified Organizational Units.

function out-CSV ( $LogFile, $Append = $false) {

# Input : An Object with values, Boolean value if to append the file or not, a File path to a Log File

# Output : Export of the object values to a CSV File

# Description : This Function Exports all the Values and Headers of an object to a CSV File.

#  The Object is recieved with the Input Const (Used with Pipelineing) or the $inputObject

Foreach ($item in $input){

# Get all the Object Properties

$Properties = $item.PsObject.get_properties()

# Create Empty Strings - Start Fresh

$Headers = ""

$Values = ""

# Go over each Property and get it's Name and value

$Properties | %{ 

$Headers += $_.Name+"`t"

$Values += $_.Value+"`t"

}

# Output the Object Values and Headers to the Log file

If($Append -and (Test-Path $LogFile)) {

$Values | Out-File -Append -FilePath $LogFile

}

else {

# Used to mark it as an Powershell Custum object - you can Import it later and use it

# "#TYPE System.Management.Automation.PSCustomObject" | Out-File -FilePath $LogFile

$Headers | Out-File -FilePath $LogFile -Encoding Unicode

$Values | Out-File -Append -FilePath $LogFile

}

}

}


$LogFile = "C:\ContactsGAL.csv"


# Collect User and Contact Details

$MailUser = Get-User -ResultSize unlimited -RecipientTypeDetails UserMailbox -OrganizationalUnit "Domain.com/Users" | select FirstName,LastName,DisplayName,WindowsEmailAddress

$MailContact = Get-Contact -ResultSize unlimited -RecipientTypeDetails MailContact -OrganizationalUnit "Domain.com/Contacts" | select FirstName,LastName,DisplayName,WindowsEmailAddress


# Create The Main Object

$ExportGAL = "" | Select "First Name","Last Name","E-mail Address","E-mail Type","E-mail Display Name"


# Merge Mail User Details to Main Object

For($i=0; $i -lt $MailUser.Length; $i++)

{

$ExportGAL.'First Name' = $MailUser[$i].FirstName

$ExportGAL.'Last Name' = $MailUser[$i].LastName

$ExportGAL.'E-Mail Address' = $MailUser[$i].WindowsEmailAddress.ToString()

$ExportGAL.'E-mail Type' = "SMTP"

$ExportGAL.'E-mail Display Name' = $MailUser[$i].DisplayName

# Export Main Object to CSV

$ExportGAL | out-csv $LogFile -append $True

}

# Merge Mail Contact Details to Main Object

For($i=0; $i -lt $MailContact.Length; $i++)

{

$ExportGAL.'First Name' = $MailContact[$i].FirstName

$ExportGAL.'Last Name' = $MailContact[$i].LastName

$ExportGAL.'E-Mail Address' = $MailContact[$i].WindowsEmailAddress.ToString()

$ExportGAL.'E-mail Type' = "SMTP"

$ExportGAL.'E-mail Display Name' = $MailContact[$i].DisplayName

# Export Main Object to CSV

$ExportGAL | out-csv $LogFile -append $True

}

 

Comments