Home » Archive » Create vCard From Active Directory

Create vCard From Active Directory

I had a requirement to export all users’ contact information from active directory to vCard so they could be imported into a mobile phone.

Turns out it is really easy once you understand the VCARD formatting. Run the following script from a server or workstation with AD Powershell Module

Import-Module ActiveDirectory
$vCardPath = "C:\mobile.vcf"
#test to see if vcard file already exists
$outputvcard = Test-Path $vCardPath 
If (!$outputvcard){
 #if not then create the file
 $outputvcard = New-Item -Path $vCardPath -ItemType File -Force
#get AD users
$ADUsers = Get-ADUser -SearchBase "OU=My users,DC=domain,DC=com" -Filter {(ObjectClass -eq "User") -and (Enabled -eq $true) } -Properties * | Select givenName,SN,Mail,Mobile,OfficePhone 
ForEach ($user in $ADUsers){ 
 Add-Content -Path $vCardPath -Value "BEGIN:VCARD"
 Add-Content -Path $vCardPath -Value "VERSION:2.1"
 Add-Content -Path $vCardPath -Value "N:$($user.SN);$($user.givenName)"
 Add-Content -Path $vCardPath -Value "FN:$($user.givenName) $($user.SN)"
 Add-Content -Path $vCardPath -Value "ORG:Company name here"
 Add-Content -Path $vCardPath -Value "TEL;WORK;VOICE:$($user.Mobile)"
 Add-Content -Path $vCardPath -Value "TEL;HOME;VOICE:$($user.OfficePhone)"
 Add-Content -Path $vCardPath -Value "ADR;WORK;PREF:Street Address here;Postcode;City;UK"
 Add-Content -Path $vCardPath -Value "EMAIL;PREF;INTERNET:$($user.Mail)"
 Add-Content -Path $vCardPath -Value "END:VCARD"

Once finished copy the vcf file to your mobile phone and import.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: