title: Handling errors with ADSI
body: <p>I'm working on a PowerShell script to change a local account name. Of course, the first step is to check that the account exists:</p>
  
  <pre><code>$user=[ADSI]"WinNT://$server/$oldName,user"
  </code></pre>
  
  <p>If the account exists, then no problem. But if it doesn't, then I get this error:</p>
  
  <blockquote>
    <p>format-default : The following exception occurred while retrieving member >"distinguishedName": "The user name could not be found."
       + CategoryInfo          : NotSpecified: (:) [format-default], ExtendedTypeSystemException
       + FullyQualifiedErrorId :
    CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand</p>
  </blockquote>
  
  <p>I can't figure out how to look for that error, report something like "$oldName not found" and continue on. From what I can tell, it isn't being thrown into an error variable, so I can't search for a "user name could not be found" string. Try-Catch-Finally seems to ignore the error.</p>
  
  <p>I admit I'm weak on error-handling. It seems there's countless ways for something to fail, and my users always find new ones when using my scripts.</p>