Our local identity management guru Jackson Shaw tasked me with giving him an easy way to export contact information from corporate address book so you can then send it to someone for their reference. The standard format for Outlook to import contact information is vCard, but the problem is that Outlook can export to vCard only personal contacts, but not GAL entries. Needless to say, PowerShell is the answer. 😉
This is the command-line which solves the task:
Get-QADUser "Dmitry Sotnikov" | Out-vCard
This will locate a user in your AD whose name is "Dmitry Sotnikov"
(which probably means you work for Quest) and create a file "Dmitry Sotnikov.vcf"
at the c:\ drive root.
If you want to export all members of a DL – this will work too:
Get-QADGroupMember DL.ProjectA | Out-vCard
This will create a vCard for each DL member.
And because it only reads data from your Active Directory you don’t need any administrative privileges. This will work for any domain user.
To make this work you need to:
- Install PowerShell and AD cmdlets.
- Copy/paste the following function into PowerShell command-line shell before running the commands or add it to your profile (My Documents/WindowsPowerShell/profile.ps1):
function Out-vCard {
$input | ForEach-Object {
$filename = "c:\" + $_.Name + ".vcf"
Remove-Item $filename -ErrorAction SilentlyContinue
add-content -path $filename "BEGIN:VCARD"
add-content -path $filename "VERSION:2.1"
add-content -path $filename ("N:" + $_.LastName + ";" + $_.FirstName)
add-content -path $filename ("FN:" + $_.Name)
add-content -path $filename ("ORG:" + $_.Company)
add-content -path $filename ("TITLE:" + $_.Title)
add-content -path $filename ("TEL;WORK;VOICE:" + $_.PhoneNumber)
add-content -path $filename ("TEL;HOME;VOICE:" + $_.HomePhone)
add-content -path $filename ("TEL;CELL;VOICE:" + $_.MobilePhone)
add-content -path $filename ("TEL;WORK;FAX:" + $_.Fax)
add-content -path $filename ("ADR;WORK;PREF:" + ";;" + $_.StreetAddress + ";" + $_.PostalCode + " " + $_.City + ";" + $_.co + ";;" + $_.Country)
add-content -path $filename ("URL;WORK:" + $_.WebPage)
add-content -path $filename ("EMAIL;PREF;INTERNET:" + $_.Email)
add-content -path $filename "END:VCARD"
}
}
Note that the script is something I put together in 15 minutes to help Jackson, so it still needs a few improvements when I have time:
- Need to add an optional parameter for the output folder.
- Need to actually look at vCard spec to make sure all attributes translate right.
- Need to look whether I need to check whether attributes are present. Does vCard format permit empty values or should their keys be in that case omitted?
Anyways, this seems to solve the task for now, I hope I have a few hours later to make it perfect. Feel free to do so yourself if you are interested.
Tags: AD, AD cmdlets, Active Directory, Examples, PowerShell, one-liner, oneliner
Very nice, I’ll put that to good use.
Can’t resist saying … you ought to write it as a filter function: use the PROCESS block so you don’t hold up the pipeline 😉 or have a look at my template pipeline script to do it the full featured way.
http://huddledmasses.org/writing-better-script-functions-for-the-powershell-pipeline/
I’m going to take a long shot here, looking at the age of the post. I have followed the instructions and am very happy with the results. Our company uses the JPEGPHOTO AD object to store a photo of every user. I’m trying to modify the OUT-VCARD function to pull this info in, but I’m not having much luck with it. This is what I have so far:
add-content -path $filename (“PHOTO; TYPE=JPEG; ENCODING=base64;:” + $_.JPEGPHOTO)
Any insights or help are appreciated.