Store Pictures in User Accounts

by Jun 30, 2010

Have you ever wondered how PowerShell would store a jpeg picture into your Active Directory user account? With the help of some low-level .NET calls, it's doable. You should simply adjust the path to your picture and the LDAP path to your user account:

$file = 'C:bild.jpg'
$userpath = 'LDAP://mydc01/CN=Tobias,CN=Users,DC=powershell,DC=local'
$pic = New-Object system.Drawing.Bitmap($file)

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

$user = New-Object System.DirectoryServices.DirectoryEntry($userpath)
$User.Properties["jpegPhoto"].Value = $byte
$user.SetInfo()