none
How i can update thumbnailPhoto AD attribute with powershell?

    Question

  • I have jpg file. How with powershell a can update thumbnailPhoto attribute in Active Directory with this jpg file?
    Wednesday, September 23, 2009 9:38 AM

Answers

  • Nope, there's no parameter by that name :)

    Actually I have a draft post sitting on my scripts folder (for too long):


    [byte[]]$jpg = Get-Content c:\photo.jpg -encoding byte
    $user = [adsi]"LDAP://cn=user1,cn=users,dc=domain,dc=loc"
    $user.Properties["jpegPhoto"].Clear()
    $null = $user.Properties["jpegPhoto"].Add($jpg)
    $user.CommitChanges()

     


    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Wednesday, September 23, 2009 4:45 PM
    Moderator
  • [byte[]]$jpg = Get-Content c:\photo.jpg -encoding byte

    Thanx for this string.


    Work code for me:

    $username = "maxx"
    $jpgfile = "d:\dsc_2250.jpg"
    $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $root = $dom.GetDirectoryEntry()
    $search = [System.DirectoryServices.DirectorySearcher]$root
    $search.Filter = "(&(objectclass=user)(objectcategory=person)(samAccountName=$username))"
    $result = $search.FindOne()

    if ($result -ne $null)
    {
     $user = $result.GetDirectoryEntry()
     [byte[]]$jpg = Get-Content $jpgfile -encoding byte
     $user.put("thumbnailPhoto",  $jpg )
     $user.setinfo()
     Write-Host $user.displayname " updated"
    }
    else {Write-Host $struser " Does not exist"}

    • Marked as answer by maxx-ode Wednesday, September 30, 2009 12:06 PM
    Wednesday, September 30, 2009 12:06 PM
  • I can't test now ,see if this helps:

    $user = [adsi]"LDAP://cn=user1,cn=users,dc=domain,dc=loc"
    $user.Properties["jpegPhoto"].Clear()

    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Wednesday, January 13, 2010 7:57 PM
    Moderator
  • IIRC the thumbnailPhoto attribute has a 10k size limit (recommended size 96x96 pixels). The image you're using is bigger than than that?

    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Friday, July 09, 2010 12:33 PM
    Moderator

All replies

  • Perhaps with quest AD cmdlets you coud do:

    Set-QADUser -Identity "user" -thumbnailPhoto "path to photo"

    I cannot test this, as we don't use that attribute.

    Karl
    Wednesday, September 23, 2009 2:50 PM
  • Nope, there's no parameter by that name :)

    Actually I have a draft post sitting on my scripts folder (for too long):


    [byte[]]$jpg = Get-Content c:\photo.jpg -encoding byte
    $user = [adsi]"LDAP://cn=user1,cn=users,dc=domain,dc=loc"
    $user.Properties["jpegPhoto"].Clear()
    $null = $user.Properties["jpegPhoto"].Add($jpg)
    $user.CommitChanges()

     


    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Wednesday, September 23, 2009 4:45 PM
    Moderator
  • [byte[]]$jpg = Get-Content c:\photo.jpg -encoding byte

    Thanx for this string.


    Work code for me:

    $username = "maxx"
    $jpgfile = "d:\dsc_2250.jpg"
    $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $root = $dom.GetDirectoryEntry()
    $search = [System.DirectoryServices.DirectorySearcher]$root
    $search.Filter = "(&(objectclass=user)(objectcategory=person)(samAccountName=$username))"
    $result = $search.FindOne()

    if ($result -ne $null)
    {
     $user = $result.GetDirectoryEntry()
     [byte[]]$jpg = Get-Content $jpgfile -encoding byte
     $user.put("thumbnailPhoto",  $jpg )
     $user.setinfo()
     Write-Host $user.displayname " updated"
    }
    else {Write-Host $struser " Does not exist"}

    • Marked as answer by maxx-ode Wednesday, September 30, 2009 12:06 PM
    Wednesday, September 30, 2009 12:06 PM
  • Anybody know how to remove it via Script?
    Seems to have a hard time in ADSIEdit removing the value back to <Not Set>

    Then there is the question of the cached images?  Anyway to force them to refresh to <Not Set>??

    Thanks... Minor Firestorm here over this item...
    Wednesday, January 13, 2010 6:33 PM
  • I can't test now ,see if this helps:

    $user = [adsi]"LDAP://cn=user1,cn=users,dc=domain,dc=loc"
    $user.Properties["jpegPhoto"].Clear()

    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Wednesday, January 13, 2010 7:57 PM
    Moderator
  • The .Clear Method...  Damn should have thought of that.

    Yep it worked.

    Thank you.
    Thursday, January 14, 2010 2:13 PM
  • Here is some code to read the image out of the AD.

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "My Photo"
    $objForm.Size = New-Object System.Drawing.Size(300,200)
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
        {$x=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
        {$objForm.Close()}})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)

    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(10,20)
    $objLabel.Size = New-Object System.Drawing.Size(280,20)
    $objLabel.Text = "Enter name of user"
    $objForm.Controls.Add($objLabel)

    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,40)
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objForm.Controls.Add($objTextBox)

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})
    [void] $objForm.ShowDialog()

    $user = [adsi]"LDAP://CN=$x,OU=xx,DC=xx,DC=xx,DC=xx"

    $bytes = $user.Properties["jpegPhoto"].Value

    [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    $form = new-object Windows.Forms.Form
    $form.Text = "Image Viewer"
    $form.AutoSize = "True"
    $form.AutoSizeMode = "GrowAndShrink"
    $pictureBox = new-object Windows.Forms.PictureBox
    $pictureBox.SizeMode = "AutoSize"

    $pictureBox.Image = $bytes
    $form.controls.add($pictureBox)
    $form.Add_Shown( { $form.Activate() } )
    $form.ShowDialog()

    Friday, January 22, 2010 8:44 AM
  • Even though this is an old post, here is how you can do it with Set-QADuser

     

    $photo = [byte[]](Get-Content C:\Picture.jpg  -Encoding byte)
    Set-QadUser <username> -ObjectAttributes @{thumbnailPhoto=$photo}

    • Proposed as answer by Marc Costanzo Tuesday, November 05, 2013 3:18 PM
    Tuesday, July 06, 2010 8:50 AM
  • I tried this, but I get an error:

    Set-QADUser : A contraint violation occurred.
    At line:1 char:12
    + Set-QADUser <<<< "mydom\test1" -ObjectAttributes @{thumbnailPhoto=$photo}
        +CategoryInfo : NotSpecified: (:) [Set-QADUser], DirectoryServicesCOMException
        + FullyQualifiedErrorId : System.DirectoryServices.DirectoryServicesCOMException,Quest.ActiveRoles.ArsPowerShellSnapIn.PowerShell.Cmdlets.SetUserCmdlet

    Any idea?

    Thanks,
    Zsolt

    Friday, July 09, 2010 11:59 AM
  • IIRC the thumbnailPhoto attribute has a 10k size limit (recommended size 96x96 pixels). The image you're using is bigger than than that?

    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar
    Friday, July 09, 2010 12:33 PM
    Moderator
  • Indeed. I tested it with a 3k file and it works.

    Thanks,
    Zsolt

    Friday, July 09, 2010 12:36 PM
  • Anyone know if the 10k photos can be uploaded with the uploadphoto attribute in an environment that has Exchange 2007, Active Directory forest\domain 2008 functional level and outlook 2010 with cache mode enabled?

    Appreciate the support.

    Wednesday, August 11, 2010 1:15 AM
  • IIRC the thumbnailPhoto attribute has a 10k size limit (recommended size 96x96 pixels). The image you're using is bigger than than that?

    Shay Levy [MVP]
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar

    Sorry I know this thread hasn't been updated for a month or two but it is one of the first results on google when looking for information on the thumbnailPhoto limits so I just wanted to correct something that people seem to have got wrong in every single article/post on the internet about this attribute (and I've no idea why because it hasn't changed since it was created in Windows 2000).

    The thumbnailPhoto attribute is not limited to 10 KB, it is limited to 100 KB.

    Don't believe me? The maximum value for that attribute is shown as 102400 bytes (either look in the AD schema yourself or look at the RangeUpper value here: http://msdn.microsoft.com/en-us/library/ms680034(VS.85).aspx ) and if you divide 102400 by 1024 to get the number of KB that this represents, you get 100 not 10. To further prove this theory, I just tried uploading an image to this attribute that was 90 KB and it worked fine - I then tried an image that was 120 KB and got the constraint error that you would expect.

    Now I don't know if the Powershell cmdlets that come with Exchange 2010 limit you to only selecting an image that is less than 10 KB but as we only have Exchange 2003 I can't test that very easily. The way I upload images to this attribute in our AD is using a program that I'm making purely for this purpose (don't worry it will be free and it is a nice easy to use GUI app, not command line) - if anyone is interested in using it you can find out more info on my blog here: http://cjwdev.wordpress.com/2010/10/31/ad-photo-edit-a-program-for-uploading-images-into-the-active-directory-thumbnailphoto-attribute-used-by-outlook-2010/

    EDIT: I mentioned this to the exchange team and they have said they will update their blog post about the Outlook 2010 photo feature to reflect the correct size :) They did also confirm that the powershell cmdlets specifically limit you to 10 KB though (the program I'm making that I mentioned above does not do this by the way).


    My website: www.cjwdev.co.uk My blog: cjwdev.wordpress.com
    Tuesday, November 02, 2010 9:51 PM
  • Thanks for the update!
    Shay Levy [MVP]
    PowerShay.com
    PowerShell Toolbar
    Wednesday, November 17, 2010 5:18 PM
    Moderator
  • you're absolutely correct, AD has a limit of 100KB for thumbnailphoto, Exchange 2010 limit this to 10KB and the Sharepoint server limit is 40KB. The full recomendation is keep the size as low as possible to avoid global catalog replication problems (you have to sum 100KB per every user with photo added, and all this info has to be replicated through all GC servers around your AD domain). 

    10KB and 96x96 pix seems a good size for me.


    www.comunicacionunificada.cl



    Wednesday, February 22, 2012 2:45 PM
  • Hi,

    I was solving task for bulk update of multiple resource objects having the same thumbnail displayed in LYNC, these accounts didn't have mailbox associated so I can't use Exchange powershell cmdlets like

    Import-RecipientDataProperty -Identity username -Picture -FileData ([Byte[]]$(Get-Content -path "JPGFilePath.jpg" -Encoding Byte -ReadCount 0))

    Importing to AD using native AD cmdlets can be done like this

    [byte[]]$jpg = Get-Content JPGFilePath.jpg -encoding byte

    Get-ADUser -filter {displayname -like "resourceaccountsprefix*"} | set-aduser -replace @{thumbnailphoto=$jpg}


    BTW I foung easy bulk conversion and resize of jpg files in this article - http://www.stevieg.org/tag/optimization/

    Hope this helps.

    Michal

    Thursday, April 12, 2012 9:59 PM
  • Personally I used this app. 

    https://play.google.com/store/apps/details?id=com.infradog.serveradmin&hl=en, it lets me upload photo from my phone photo gallery or just take one and upload. (available on iOS also?)

    Here is a video that shows how to use this app to upload AD photo, it worked for me.

    https://www.youtube.com/watch?v=y9soNI1ymfA

    Maybe it can help you too.

    Friday, November 08, 2013 8:29 PM