How to set user profile picture using PowerShell

  • Last update on January 25th, 2024

If you are looking for a way to import user photos to Exchange 0365 in bulk through SharePoint site, you can use the following custom action:

The logic behind is to create a photo gallery where you will upload the photos, and you could make the filename of each image be the username of that particular user.

$myUrlSite = "https://<Domain>.sharepoint.com"
 $format = ".jpeg"
 #retrieve the current ps credential object from the sharepoint connection
 $cred = Get-PnPConnection
 #here you have to change the site context because we use the admin site when we open a Sharepoint connection
 Connect-PnPOnline -Url $myUrlSite -Credentials ($cred.PSCredential)
 $photos = @(Get-PnPFolderItem -FolderSiteRelativeUrl "photos" -ItemType File)
 foreach( $photo in $photos){
 if( $photo.Name.Contains("@<Domain name>.com")){
 $memoryStream = New-Object System.IO.MemoryStream
 $streamResult = $photo.OpenBinaryStream()
 Invoke-PnPQuery
 $streamResult.Value.CopyTo( $memoryStream)
 $imageArray = $memoryStream.ToArray()
 Set-UserPhoto -Identity $photo.Name.Replace($format, '') -PictureData $imageArray -Confirm: $false
 }
 }