In researching for my own needs I came across this thread and was able to make the answer work by performing the following edits.
On line 1, I added a SearchBase
filter to help narrow down my targets. I also selected both the name
and SamAccountName
attributes for later use.
On line 5, I changed the item $user
to $user.Name
so that the formatting would look as intended and so that the comparison would evaluate to True. This same adjustment is made to lines 6, 7, and 12.
On line 7, be sure to use a consistent file extension and adjust the path to be where your photos are stored. Best to stick to either .png or .jpg. Remember, there is a 100KB or 96x96 pixels limit for AD photos.
On line 9, the code is adjusted from $user
to user.SamAccountName
so that the account can be discovered. In my environment, name
wasn't a valid option.
I also added some additional logging to the end of the code so you can compare the number of accounts without a photo pre and post run. Optionally and omitted from my response, this data can then be exported to CSV or some other file for record keeping or auditing.
The full adjusted code from @FoxDeploy's answer is below:
$usersWithoutImage = Get-ADUser -Filter * -SearchBase "OU=Example,DC=ACME,DC=CORP" -Properties thumbnailPhoto | ? {(-not($_.thumbnailPhoto))}
| select Name,SamAccountName
$repPics = (Get-childItem \\web01\rep-pics).basename
Write-host "Found $($usersWithoutImage.Count) users without a photo"
ForEach ($user in $usersWithoutImage){
if ($repPics -contains $user.Name){
Write-host "Users name $($user.Name) is in the users photo directory, uploading..."
$imagePath = ".\$($user.Name).png"
$ADphoto = [byte[]](Get-Content $imagePath -Encoding byte)
Set-ADUser $user.SamAccountName -Replace @{thumbnailPhoto=$ADphoto}
}
else{
Write-Warning "Users name $($user.Name) is NOT in the users photo directory, please update!!"
}
}
$usersWithoutImage = Get-ADUser -Filter * -Properties thumbnailPhoto | ? {(-not($_.thumbnailPhoto))} | select Name
Write-Host "Found $($usersWithoutImage.Count) users without a photo since run!"