Import Exchange photos script

Posted August 2, 2018

Last updated February 23, 2019 | bc0caa8


2 minute read

This script is useful for setting multiple Exchange photos at once. In our still on-prem deployment, this is probably the quickest way for us to set pictures in batches when we have new hires.

This script is an amalgamation of a few different scripts I use in production, but mostly based off of a script from user Richard Diphoorn at powershell.org forums.

My specific iteration of the task that works for our environment is copying the photo to the local user’s pictures directory, and then running this script to pull those files in. The key is to have the file name be the user’s AD username, for example Rob Smith’s picture would be named rsmith.jpg and live in the current user’s Pictures folder.

 Note: Be sure to read the disclaimer before running any of the commands or scripts below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$admUser = "$env:USERNAME"
$imageFolder = "C:\Users\$admUser\Pictures"
$pictures = Get-ChildItem $imageFolder

foreach ($picture in $pictures){

    Try {
        $user = Get-User -Identity $($picture.BaseName) -ErrorAction Stop
        }

    Catch {
        Write-Warning "Warning: $_"
    }

    If ($user) {
        $user | Set-UserPhoto -PictureData ([System.IO.File]::ReadAllBytes("$imageFolder\$($picture.Name)")) -Confirm:$false
    }
}

As long as the picture is around ~250kb or less, it is lightening fast and can set a couple hundred pictures in just about two minutes by my tests.

Happy Set-UserPhotoing!