Catch errors with Try-Catch

PHOTO EMBED

Wed Dec 28 2022 01:17:57 GMT+0000 (Coordinated Universal Time)

Saved by @savabeh191

$users.ForEach{
  Try{
    # Find the user to update
    $ADUser = Get-AzureAdUser -SearchString $_.name
    
    # Update the job title
    Set-AzureAdUser -ObjectId $ADUser.ObjectId -JobTitle $_.jobtitle
  }
  Catch{
    Write-Host ("Failed to update " + $($_.name)) -ForegroundColor Red
  }
}
content_copyCOPY

https://lazyadmin.nl/powershell/powershell-script/