Share via


Upload user photos in AD using PowerShell

In some scenarios due to some issue in the user profile sync we often end up loosing profile pictures on the users mysite i.e. initially you are exporting pictures from SP to AD but one fine day you lost the sync connection and created a new one without mapping the export for the profile picture, result it will mark picture URL in the userprofile_ful table as NULL for all the users. Only way out of this situation is to re-upload the profile pictures in SP and remap the property to export and do a fullsync or best way to keep user photos is in AD and importing them back in SP. 

Following script uploads the userphotos in bulk to your AD, assumption is that photos are saved in file system as <SAMAccountname>.<extension>

========================

$path = "C:\Users\administrator.CORP\Desktop\photo"

$x = Get-ChildItem $PATH

$x.VersionInfo.filename
foreach ($item in Get-ChildItem $path)
{
$photopath = $item.versioninfo.filename
$photo = [byte[]](get-content $photopath -Encoding byte)
$temp = $photopath.lastindexof(".")
$temp1 = $photopath.LastIndexOf("\")
$temp2 = $temp-$temp1
$user = $photopath.substring($temp1+1,$temp2-1)
$user
Set-ADUser $user -Replace @{thumbnailPhoto=$photo}

}

=============================================