Wednesday 21 June 2017

Better update your photo with powershell for windows Outlook/Lync/Skype-for-business/etc

Yesterdays post required the Remote Server Administration Toolskit installed.

Here I show how to do it without that, just using powershell. Thanks to maxx-ode who gives the answer on this Microsoft Technet Forum post .

This code will set picture.jpg as your thumbnail picture.

$root = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().GetDirectoryEntry()
$search = [System.DirectoryServices.DirectorySearcher]$root
$search.Filter = "(&(objectclass=user)(objectcategory=person) (samAccountName=$env:username))"
$result = $search.FindOne()
$user = $result.GetDirectoryEntry()
$user.put("thumbnailPhoto", [byte[]](Get-Content "picture.jpg" -Encoding byte))
$user.setinfo()

or you want to delete the photo:

$user.putex(1, "thumbnailPhoto", "")
$user.setinfo()

but if you are re-using $user you may want to precede with: $user.refreshcache()

If you get an error like this:

Exception calling "setinfo" with "0" argument(s): "A constraint violation occurred."
At line:1 char:1
+ $user.setinfo()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

then it could be that your picture is too big, keep it less that 100K, and ideally much smaller than that.
A 100x100 picture should be around 4K which is more ideal.


2 comments:

  1. $result = ([ADSISEARCHER]$("samaccountname=$env:username")).findone()

    $user = $result.GetDirectoryEntry()

    $user.put("thumbnailPhoto", [byte[]](Get-Content "photograph.jpg" -Encoding byte))

    $user.setinfo()

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete