PowerShell to Import User Photos into AD and Office365

30 Mar

The PowerShell runs daily and updates all user photos with the one stored in the local directory in the format of username.jpg.

$location = 'C:\User_Photos'

$Dir = get-childitem $location
$List = $Dir | where {$_.extension -eq ".jpg"}

#loop all JPG's
foreach ($item in $List)
    $filename = $item.Name
    $username = $filename.split('\.')[-2]
    write-host "Found photo $location\$filename"

    write-host "Processing $username"

    #Populate AD
    write-host "Adding photo to AD for $username"
    SET-ADUser $username –add @{thumbnailphoto=$Picture}
    #Populate Office365
    write-host "Adding photo to Office365 for $username"
    Set-UserPhoto $username -PictureData ([System.IO.File]::ReadAllBytes("$location\$filename")) -Confirm:$Y

All photos are 96 x 96 pixels, JPG format and <10KB in size.

Then to prevent users from editing their own information (so it’s centrally controlled), you can change the default user roles in Exchange Admin:

2388.disable photo

You can also use AdPhotoEdit to quickly see and edit user photos if you like.