This script helps you export contacts from a mailbox using Microsoft Graph, but keep in mind it hasn’t been fully tested, so proceed with caution. The process involves setting up a destination folder for export, prompting the user for an email address, and connecting to Microsoft Graph with proper authentication. It includes a function to make API calls and loops through the user’s contact folders to export the contact information to CSV files. The script is flexible, allowing you to specify a particular folder or work with the root folder. Make sure the Microsoft Graph module is installed and configured properly before running the script, and always verify results to ensure accuracy.
# Import Microsoft Graph module (ensure module is installed first)
Import-Module Microsoft.Graph
# Set export destination folder
$exportDestination = "C:\temp"
# Prompt for user input and ensure the email ends with "@domain"
do {
$users = Read-Host -Prompt "Please enter the user's email address (must end with @domain)"
} until ($users -match '@domain\.com$')
Write-Output "You entered: $users"
# Specify contact list folder to export
$folder = "contacts"
# Connect to Microsoft Graph with user authentication
Connect-MgGraph -Scopes "User.Read", "Contacts.Read"
# Function to make Graph API calls
function Get-GraphRequest {
param (
[string]$uri
)
Process {
try {
$response = Invoke-MgGraphRequest -Method GET -Uri $uri
return $response
}
catch {
Write-Warning "Failed to complete request, Error message: $($_.Exception.Message)"
}
}
}
foreach ($user in $users) {
if ($folder -eq 'root') {
$userContactFolders = Get-GraphRequest -uri "https://graph.microsoft.com/v1.0/users/$user/contactfolders/contacts"
foreach ($userContactFolder in $userContactFolders) {
$contacts = Get-GraphRequest -uri "https://graph.microsoft.com/v1.0/users/$user/contactfolders/$($userContactFolder.id)/contacts"
$customContacts = @()
foreach ($contact in $contacts) {
$myObject = [PSCustomObject]@{
Name = $contact.displayName
Email = ($contact.emailAddresses).name
}
$customContacts += $myObject
}
# Export to CSV using the defined export destination
$customContacts | Export-Csv "$exportDestination\$user-root.csv" -NoTypeInformation
}
}
else {
$userContactFolders = Get-GraphRequest -uri "https://graph.microsoft.com/v1.0/users/$user/contactfolders" | Where-Object { $_.displayName -like "$folder*" }
foreach ($userContactFolder in $userContactFolders) {
$contacts = Get-GraphRequest -uri "https://graph.microsoft.com/v1.0/users/$user/contactfolders/$($userContactFolder.id)/contacts"
$customContacts = @()
foreach ($contact in $contacts) {
$myObject = [PSCustomObject]@{
Name = $contact.displayName
Email = ($contact.emailAddresses).name
}
$customContacts += $myObject
}
# Export to CSV using the defined export destination
$customContacts | Export-Csv "$exportDestination\$user-$($userContactFolder.displayName).csv" -NoTypeInformation
}
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Sorry! The Author has not filled his profile.