SCRIPT: UploadPhoto.ps1

Script Name: UploadPhoto.ps1

Script Author: Anderson Patricio, MVP

Original Post:

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 (<a href=""></a>)
# 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)) } 

One thought on “SCRIPT: UploadPhoto.ps1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.