title: Exchange Online - Get-UserPhoto - how to catch non terminating error?
body: <p>I'm trying to export a list of all users with no photo from our Exchange Online account using powershell. I cannot get it to work and have tried various methods. </p>
  
  <p>Get-UserPhoto returns this exception when there is no profile present. </p>
  
  <pre><code>Microsoft.Exchange.Data.Storage.UserPhotoNotFoundException: There is no photo stored here.
  </code></pre>
  
  <p>First of all I tried use <strong>Errorvariable</strong> against the command but received:</p>
  
  <pre><code>A variable that cannot be referenced in restricted language mode or a Data section is being referenced. Variables that can be referenced include the following: $PSCulture, $PSUICulture, $true, $false, and  $null.
  + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
  + FullyQualifiedErrorId : VariableReferenceNotSupportedInDataSection
  + PSComputerName        : outlook.office365.com
  </code></pre>
  
  <p>Next I tried  <strong>try, catch</strong> but the non-terminating error never calls the <strong>catch</strong> despite various methods followed online about setting <strong>$ErrorActionPreference</strong> first of all.</p>
  
  <p>Any ideas ? Here is the script:</p>
  
  <pre><code> $UserCredential = Get-Credential
  $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
  Import-PSSession $Session 
  
  $timestamp = $timestamp = get-date -Format "dd/M/yy-hh-mm"
  $outfile = "c:\temp\" + $timestamp + "UserswithoutPhotos.txt"
  
  
  
  
  $resultslist=@()
  $userlist = get-user -ResultSize unlimited -RecipientTypeDetails usermailbox | where {$_.accountdisabled -ne $True}
  
  
  Foreach($user in $userlist)
  {
      try
      {
  
          $user | get-userphoto -erroraction stop 
      }
      catch
      {
          Write-Host "ERROR: $_"
          $email= $user.userprincipalname
          $name = $user.DisplayName
          $office = $user.office
          write-host "User photo not found...adding to list : $name , $email, $office" 
          $resultslist += $user
  
      }
  }
  $resultslist | add-content $outfile 
  $resultslist
  </code></pre>