User pictures in AD
Adding pictures to Active Directory is kind of nice, it gives you pictures in Outlook, Lync and few other products…
To add it you need to load a picture as binary and put it in to the attribute thumbnailPhoto on the user.
Function Add-AdThumbnailPhoto { PARAM ( [ValidateScript({Test-Path $_ -PathType Leaf})] [string] $PicturePath, $UserAccount ) If (!(Test-IsModuleLoaded "ActiveDirectory")) { Throw "You need to run: Import-Module ActiveDirectory" } Write-Verbose "Adding $($PicturePath) to $($UserAccount)" $pictureBinary = [byte[]](Get-Content $PicturePath -Encoding byte) If ([System.Text.Encoding]::ASCII.GetString($pictureBinary).Length -ge 100Kb) { Throw "Picture to large, max size is 100Kb" } Try { Set-AdUser $UserAccount -Replace @{ thumbnailPhoto = $pictureBinary } } Catch { Throw $error[0] } }
Then you can use the function like this:
Add-AdThumbnailPhoto -PicturePath "C:\MyPicture.jpg" -UserAccount "MyUserAccount"
If you like to do it on a oneliner… here is a bit more compressed version of the same code:
Set-AdUser "MyUserAccount" -Replace @{ thumbnailPhoto = ([byte[]](Get-Content "C:\MyPicture.jpg" -Encoding byte) }