SCRIPT: UploadPhoto.ps1

Share this:

Script Name: UploadPhoto.ps1 v1

v1.0 – Initial script. Check jpg file size and path. Uses a standard path as jpg repository and allows to add pictures to a single user or multiple users (-all switch)


# Script created by Anderson Patricio (
# Syntax:
# uploadphoto.ps1 <mailbox-name>
# uploadphoto.ps1 -all (It will load all jpg files from the default folder and upload to the users in AD

param([Switch]$all, [String]$UserName)

#Default Values. Change them based on your environment.
$DefaultPhotoPath = ‘C:Photos’

Function CheckPhoto(){
 Write-Warning “Validating file(s)..”
 Write-Host “File exists… ” -nonewline
 If (Test-Path $PhotoPath)
  Write-Host “[OK]” -ForeGroundColor Green
  Write-host “Photo size… ”  -nonewline
  $PhotoSize = Get-ChildItem $PhotoPath | select Length
  If ($PhotoSize.Length -le 10000) { Write-Host “[OK]” -ForeGroundColor Green } Else { Write-Host “[Fail]” -ForeGroundColor Red; exit }
  Write-Host “[Fail]” -ForeGroundColor Red

Function UploadAll(){
 ForEach ($TempFile in Get-ChildItem $DefaultPhotoPath | Where-Object { $_.Extension -eq “.jpg” } )
   $TempUserName = $TempFile.Name.substring(0, $TempFile.Name.Length – 4)
   Write-Host $TempUserName -ForeGroundColor Yellow -NoNewLine
   Import-RecipientDataProperty -Identity $TempUserName -Picture -FileData ([Byte[]]$(Get-Content -path $TempFile.Fullname -Encoding Byte -ReadCount 0))
   Write-Host “[Done]” -ForeGroundColor Green

If ( $all -eq $true)
 Write-Warning ” ## This action will upload all pictures of C:Photos to the AD users.”
 Write-Warning ” ## All pictures must have the same name of the usernames”
 Write-Warning “Are you sure that you want upload all pictures to the users (Y/N)?”
 $Opt = Read-Host
 If ( $Opt -eq ‘y’ ) { UploadAll; } Else { Write-Host “No changes were made.”; Exit }
 $PhotoPath = $DefaultPhotoPaty + $UserName + ‘.jpg’
 If ( $AbortMission -eq ‘$true’ ) { Write-Error “Please, review the errors and try again.” } Else { Import-RecipientDataProperty -Identity $UserName -Picture -FileData ([Byte[]]$(Get-Content -path $PhotoPath -Encoding Byte -ReadCount 0)) } 

Written by Anderson Patricio

Anderson Patricio is a Canadian Exchange Server MVP and MCSM (Solutions Master) and he contributes to the Microsoft Community with articles, tutorials, blog posts, forums and book reviews. He is a regular contributor at, and (Portuguese).