Managing contacts in Microsoft 365 is an essential task for many organizations, especially when handling large amounts of contact data for specific users. This PowerShell script, powered by Microsoft Graph API, provides an interactive way to perform key contact management tasks: listing, adding, updating, and deleting contacts for a user. The script offers a convenient console-based approach, making it easy for admins to manage user contacts directly from PowerShell.
# Microsoft Graph API connection
Connect-MgGraph -Scopes "Contacts.ReadWrite"
Write-Host "Connected to Microsoft Graph"
# Prompt for User ID
$userId = Read-Host "Enter the User ID (or UserPrincipalName) of the user"
# Function to list contacts in a tabular format
function List-Contacts {
Write-Host "Listing contacts for User ID: $userId..."
$contacts = Get-MgUserContact -UserId $userId
if ($contacts) {
$contacts | Select-Object DisplayName, GivenName, Surname, @{Name="Email";Expression={$_.EmailAddresses[0].Address}} | Format-Table -AutoSize
} else {
Write-Host "No contacts found for the specified user."
}
}
# Function to add a new contact
function Add-Contact {
$displayName = Read-Host "Enter the contact's display name"
$givenName = Read-Host "Enter the contact's given name"
$surname = Read-Host "Enter the contact's surname"
$email = Read-Host "Enter the contact's email address"
$contactParams = @{
DisplayName = $displayName
GivenName = $givenName
Surname = $surname
EmailAddresses = @(@{ Address = $email; Name = $displayName })
}
$newContact = New-MgUserContact -UserId $userId -BodyParameter $contactParams
Write-Host "Contact '$displayName' added successfully with ID: $($newContact.Id)"
}
# Function to update an existing contact
function Update-Contact {
$contactId = Read-Host "Enter the Contact ID of the contact you want to update"
$newDisplayName = Read-Host "Enter the new display name for the contact (leave blank to skip)"
$newEmail = Read-Host "Enter the new email address for the contact (leave blank to skip)"
$updateParams = @{}
if ($newDisplayName) { $updateParams.DisplayName = $newDisplayName }
if ($newEmail) {
$updateParams.EmailAddresses = @(@{ Address = $newEmail; Name = $newDisplayName })
}
if ($updateParams.Count -gt 0) {
Update-MgUserContact -UserId $userId -ContactId $contactId -BodyParameter $updateParams
Write-Host "Contact updated successfully."
} else {
Write-Host "No updates were made."
}
}
# Function to remove a contact
function Remove-Contact {
$contactId = Read-Host "Enter the Contact ID of the contact you want to delete"
$confirmation = Read-Host "Are you sure you want to delete this contact? (y/n)"
if ($confirmation -eq "y") {
Remove-MgUserContact -UserId $userId -ContactId $contactId -Confirm:$false
Write-Host "Contact deleted successfully."
} else {
Write-Host "Contact deletion canceled."
}
}
# Main Script Loop
while ($true) {
Write-Host "`nChoose an action to manage user contacts:"
Write-Host "1. List Contacts"
Write-Host "2. Add a Contact"
Write-Host "3. Update a Contact"
Write-Host "4. Remove a Contact"
Write-Host "5. Exit"
$choice = Read-Host "Enter the number corresponding to your choice"
switch ($choice) {
"1" { List-Contacts }
"2" { Add-Contact }
"3" { Update-Contact }
"4" { Remove-Contact }
"5" { Write-Host "Exiting..."; break }
default { Write-Host "Invalid choice, please try again." }
}
}
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Disconnected from Microsoft Graph"
See the Script in Action by clicking and playing this GIF:
Error | Cause | Solution |
NotFound: Resource not found | UserId or ContactId does not exist. | Verify UserId and ContactId with Get-MgUser and Get-MgUserContact. |
Permission Denied | Insufficient permissions. | Ensure Contacts.ReadWrite permissions. |
InvalidRequest: Invalid Parameter in Body | Improper BodyParameter structure. | Check BodyParameter formatting for valid properties. |
This PowerShell script provides a straightforward, interactive approach to managing user contacts in Microsoft 365. From listing contacts in a tabular format to adding, updating, and removing contacts, the script simplifies contact management tasks for administrators. With additional enhancements, this script can become an even more powerful tool for managing contacts at scale, enabling organizations to improve productivity and streamline user data management in Microsoft 365.
© m365corner.com. All Rights Reserved. Design by HTML Codex