Store Pictures in Active Directory

by Nov 28, 2011

When you need to store a picture into an AD account, the picture will have to be converted to byte values before it can be stored. Just make sure you adjust the path to the picture you want to store and the LDAP path of the AD object you want the picture to be stored in:

# adjust picture path:
$file = "C:\pic.jpg"

$bild = New-Object system.Drawing.Bitmap($file)

$ms = New-Object IO.MemoryStream
$bild.Save($MS, 'jpeg')
$MS.Flush()
$byte = $MS.ToArray()

# adjust user LDAP path:
$user = New-Object System.DirectoryServices.DirectoryEntry`
("LDAP://10.17.141.219/CN=Tobias,CN=Users,DC=powershell,DC=local")
$User.Properties["jpegPhoto"].Value = $byte
$user.SetInfo()

Twitter This Tip!
ReTweet this Tip!