A simple PowerShell script that imports user photos into Active Directory. After an Entra AD Sync it will push the same images to towards Azure, Office 365 (Teams, Exchange,
The user photos need to be 80×80 with a max filesize of 100kb!
Important!
$transcriptLog = "C:\Scripts\ImportThumbnail\log.txt"
if (Test-Path $transcriptLog)
{
Remove-Item $transcriptLog -Force
}
Start-Transcript -Path $transcriptLog
try
{
Import-Module ActiveDirectory
$pathDir = "C:\Scripts\ImportThumbnail\Images"
if (Test-Path $pathDir)
{
Write-Host "Path exists"
$photos = Get-ChildItem $pathDir -Filter *.jpg
$totalPhotos = (Get-ChildItem $pathDir -Filter *.jpg | Measure-Object).Count
Write-Host "Total images to be processed ${totalPhoto}"
}
else
{
Write-Host "Path does not exist"
Write-Host "Path = ${pathDir}"
}
$count = 0
$succeeded = 0
$succeededUpdating = 0
$failed = 0
$failedUpdating = 0
$deleted = 0
foreach($photo in $photos)
{
$count++
try
{
$baseName = $photo.basename
$tempBaseName = ($photo.basename -replace "'","''").ToString()
$tempPhoto = ($photo.FullName -replace "''","'").ToString()
#Write-Host "basename = $basename"
Write-Host ""
Write-Host "Processing image ${count} of ${totalPhotos}"
Write-Host ""
# Check if user is internal or EXTernal
if( (Get-ADUser -Filter "DisplayName -eq '$tempBaseName'") -Or (Get-ADUser -Filter "DisplayName -eq '$tempBaseName EXT'") )
{
# Set username base
if(Get-ADUser -Filter "DisplayName -eq '$tempBaseName EXT'")
{
$baseName = "${baseName} EXT"
}
else
{
$baseName = "${baseName}"
}
Write-Host "Setting image for ${tempBaseName}"
#Write-Host "Basename = $tempBaseName"
#Write-Host "Photo Directory Path = $($photo.DirectoryName)"
Write-Host "Photo Full Path = $($photo.FullName)"
Write-Host ""
# Select thumbnail value (if any)
$thumbnail = Get-ADUser -filter { DisplayName -eq $baseName } -Properties * | Select thumbnailPhoto
# If the user has no thumbnail yet, create a new one
if ( $thumbnail -eq $null )
{
# Try creating a new thumbnail
try
{
$tempUsernameFull = Get-ADUser -filter { DisplayName -eq $baseName } | Select SamAccountName -ExpandProperty SamAccountName
Set-ADUser -Identity $tempUsernameFull -Add @{thumbnailPhoto=([byte[]](Get-Content "${tempPhoto}" -Encoding byte))} -ErrorAction Stop
Write-Host "Succeeded creating new image ${count} of ${totalPhotos}"
$succeeded++
}
catch
{
Write-Host "Failed creating new image ${count} of ${totalPhotos}"
$failed++
}
}
# If the user has thumbnail yet, update it
elseif ( $thumbnail -ne $null )
{
# Try updating the existing thumbnail
try
{
$tempUsernameFull = Get-ADUser -filter { DisplayName -eq $baseName } | Select SamAccountName -ExpandProperty SamAccountName
if($photo.Length -le 0)
{
# Delete corrupt photo from repo
Remove-Item $photo.FullName -Recurse -Force -ErrorAction Stop
# Delete corrupt photo from AD object
Set-ADUser -Identity $tempUsernameFull -Clear thumbnailPhoto -ErrorAction Stop
Write-Host "Photo of ${tempBaseName} was corrupt, removing image ${count}"
$deleted++
}
else
{
Set-ADUser -Identity $tempUsernameFull -Replace @{thumbnailPhoto=([byte[]](Get-Content "${tempPhoto}" -Encoding byte))} -ErrorAction Stop
Write-Host "Succeeded updating existing image ${count} of ${totalPhotos}"
$succeededUpdating++
}
}
catch
{
Write-Host "Failed updating existing image ${count} of ${totalPhotos}"
$failedUpdating++
}
}
Write-Host "---"
}
# Else is the user deleted from the AD and the thumbnail is no longer needed.
else
{
Write-Host "User does not exist ${tempBaseName}, removing image"
# Try removing the thumbnail from the repository
try
{
Remove-Item $photo.FullName -Recurse -Force -ErrorAction Stop
$deleted++
Write-Host "Removed image ${count}"
Write-Host ""
}
catch
{
Write-Host "Failed to remove image ${count}"
Write-Host ""
}
Write-Host "---"
}
}
catch
{
Write-Host "Could NOT set ANY image of the total ${totalPhotos} images!"
Write-Host "An error occurred. Errorcode = $($_.Exception.Message)"
Write-Host "Please check the script!"
}
}
Write-Host ""
Write-Host "---"
Write-Host "Total to be processed images = ${totalPhotos}"
Write-Host "Succeeded creating new images = ${succeeded}"
Write-Host "Succeeded updating existing images = ${succeededUpdating}"
Write-Host "Failed creating new images = ${failed}"
Write-Host "Failed updating existing images = ${failedUpdating}"
Write-Host "Deleted images from repo = ${deleted}"
Write-Host "---"
Write-Host ""
Stop-Transcript
exit 0
}
# Exit program and catch error, return code 0x1 for failed
catch
{
Write-Host "An error occurred. Errorcode = $($_.Exception.Message)"
Stop-Transcript
exit 1
}