Forum Discussion

caseyj's avatar
caseyj
Copper Contributor
Feb 22, 2022
Solved

Testing for username not working

Hi All,   I have a code block I am trying to get working to test if a username is already created, then move to the next.  If its not existing, set variable $UPN to that username.. however, somethi...
  • Newbie_Jones's avatar
    Feb 25, 2022
    It's not going to be null though if it doesn't exist, so the catch isn't going to work. It will have the ADIdentityNotFoundException which does work in a catch as I have it in several scripts.

    However, I've done something similar to this in the past and instead of using Get-ADUser, I used Get-ADObject and a filter which means you can use null and no longer need the try\catch. (Tip: fully test your filter).

Resources