Skip to content

Instantly share code, notes, and snippets.

@chrisbrownie
Last active November 2, 2016 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisbrownie/2636a296da4599e7afae6de2862ed9c7 to your computer and use it in GitHub Desktop.
Save chrisbrownie/2636a296da4599e7afae6de2862ed9c7 to your computer and use it in GitHub Desktop.
function Get-Office365Photo($EmailAddress,$Credential) {
$wc = New-Object System.Net.WebClient
$wc.credentials = $Credential
# Build the URL that'll return the jpeg of the user's photo
$url = "https://outlook.office365.com/ews/exchange.asmx/s/GetUserPhoto?email=$EmailAddress&size=HR96x96"
# Build a path to export it to (.\kim.akers@contoso.com.jpg)
$outPath = "$pwd\$EmailAddress.jpg"
try {
# Download the image and save it to the current directory
$wc.DownloadFile($url,$outPath)
return $outPath
} catch { throw $_ }
}
function Upload-ADPhoto($Username,$FilePath) {
# Import the photo into a variable as a byte array
$photo = [byte[]](Get-Content $FilePath -Encoding byte)
# Replace the current value of thumbnailPhoto with the byte array from above
Set-ADUser $Username -Replace @{ThumbnailPhoto=$photo}
}
# Get the credential to allow us to download the images
$Cred = Get-Credential -Message "Please enter your Office 365 Credentials"
# Get every mail-enabled AD user
$users = Get-ADUser -ldapfilter '(mail=*)' -properties mail
# For each of the mail-enabled users...
foreach ($user in $users) {
try {
# Download the photo
$photoPath = Get-Office365Photo -EmailAddress $user.mail -Credential $Cred
# Upload the photo
Upload-ADPhoto -Username $user -FilePath $photoPath
} catch {
Write-Warning "Unable to update image for $($user.mail)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment