Non-terminating Error Logging

I have a script to update fields in AD, which is working well but there are user accounts that are throwing errors such as:

Set-ADUser : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADUser' required by parameter 'Identity'. Specified method is not supported.
At D:\Data\Allen\2023_Scripting\RemoteWorker_AD_OU_Switch_Full.ps1:90 char:39
+             840 {Set-ADUser -Identity $adUser.sAMAccountName -Replace ...
+                                       ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser

How might I go about logging the above with the associated user account that is producing the error in the foreach loop? I don’t want it to become a Terminating error. I’d like the scipt to continue, as it does now, but I’d like to be able to log which user accounts are having issues updating.

Thank you.

Why not? That’s actually the only right thing to do. It does not mean that your script stops working though. :man_shrugging:t3:

You simply wrap the command in a try catch block, set the -ErrorAction to Stop and log the according user account inside the catch block. :man_shrugging:t3:

Ok. I took terminating error to mean that the script would stop at that point. I will set the error action and give it a go.