forked from 12Knocksinna/Office365itpros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateUserPhotos.PS1
42 lines (41 loc) · 2.17 KB
/
UpdateUserPhotos.PS1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# UpdateUserPhotos.PS1
# A script to illustrate how to update user photos in Microsoft 365 accounts
#
# https://github.com/12Knocksinna/Office365itpros/blob/master/UpdateUserPhotos.PS1
#
Write-Host "Checking for connection to Exchange Online"
$Check = Get-Module -Name ExchangeOnlineManagement
If (!$Check) {
Write-Host "Your PowerShell session is not connected to Exchange Online."
Write-Host "Please connect to the Exchange Online Management module using an administrator account and retry."; Break }
# The value of $PhotoLocation should be changed to point to the place where user photos are stored
# We expect to find JPG files there named after the first and last names of the UPN assigned to accounts
# For example, the photo for [email protected] is in Kim.Akers.jpg
$PhotoLocation = "c:\Temp\UserPhotos\"
If (!(Test-Path ($PhotoLocation))) {
Write-Host "Can't find $PhotoLocation - please check if this is the right place to find user photos"; break }
$i=0
# Find mailboxes
Write-Host "Finding user mailboxes"
$Users = Get-ExoMailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
CLS
$ProgDelta = 100/($Users.Count); $CheckCount = 0; $UserNumber = 0
ForEach ($User in $Users) {
$PhotoExists = $Null
$UserNumber++
$UserStatus = $User.DisplayName + " ["+ $UserNumber +"/" + $Users.Count + "]"
Write-Progress -Activity "Checking photo for" -Status $UserStatus -PercentComplete $CheckCount
$CheckCount += $ProgDelta
# Is EXODS happy with the user photo information for the account?
$CheckPhoto = Get-UserPhoto -Identity $User.Alias -ErrorAction SilentlyContinue
If (!$CheckPhoto) { # No photo found in mailbox
$UserPhoto = $PhotoLocation + $User.UserPrincipalName.Split("@")[0]+".jpg"
If (Test-Path $UserPhoto) { # Update the photo because we have a file
Write-Host "Updating photo for" $User.DisplayName -Foregroundcolor Red
Set-UserPhoto -Identity $User.Alias -PictureData ([System.IO.File]::ReadAllBytes($UserPhoto)) -Confirm:$False
$i++; CLS }
Else { # No photo file available
Write-Host "No photo file available for" $User.DisplayName }
}
}
Write-Host "All done. $i User Photos updated"