Forum Discussion
caseyj
Feb 22, 2022Copper Contributor
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...
- Feb 25, 2022It'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).
Newbie_Jones
Feb 25, 2022Brass Contributor
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).
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).