How to Set User Profile Picture using PowerShell

Modified on Thu, 03 Nov 2022 at 09:31 AM

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>"
  $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()
      $streamResult.Value.CopyTo( $memoryStream)
      $imageArray = $memoryStream.ToArray()
      Set-UserPhoto -Identity $photo.Name.Replace($format, '') -PictureData $imageArray -Confirm: $false

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article