Categories
How-To Software Technical

Exchange 2010 Outlook AD photo import/export

To export a photo from AD use the following command:

$a= Get-ADUser USERNAME -Properties thumbnailphoto
$a.thumbnailphoto | Set-Content c:pathphoto.jpg -Encoding byte

To search all users and export their attribute of jpegPhoto if you want to use them for thumbnailPhoto attribute, use the following script taken from here http://social.technet.microsoft.com/Forums/ar-SA/ITCG/thread/4a1c0537-493c-409f-8ed3-b0798ad9f768

$filter = ‘(&(objectCategory=Person)(objectClass=User))’
$root= New-Object System.DirectoryServices.DirectoryEntry(“LDAP://RootDSE“)
$searcher = New-Object System.DirectoryServices.DirectorySearcher $filter

# filter only users from a certain OU (Remove comment character to enable)
#$searcher.searchroot = [adsi]‘LDAP://OU=test,DC=domain,DC=com’

$searcher.findall()  | foreach {
$user = [adsi]$_.path
$sAMAccountName = $user.psbase.Properties[“samaccountname”]
$user.psbase.Properties[“jpegPhoto”] | set-content “c:adminpics$sAMAccountName.jpg” -Encoding byte
}

You can import the photos individually using one simple line:

Import-RecipientDataProperty -Identity “USERNAME” -Picture -FileData ([Byte[]]$(Get-Content -Path “E:USERPICTURE.JPG” -Encoding Byte -ReadCount 0))

To remove a photo use the following:

Set-Mailbox USERNAME -removepicture