Check if user already exists

%3CLINGO-SUB%20id%3D%22lingo-sub-2784891%22%20slang%3D%22en-US%22%3ECheck%20if%20user%20already%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2784891%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20there%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20create%20a%20script%20which%20basically%20requires%20the%20user's%20input%2C%20so%20they'll%20need%20to%20enter%20their%20first%20name%20%26amp%3B%20last%20name%20and%20AD%20will%20check%20if%20that%20AD%20account%20already%20exists%20for%20the%20user%20or%20not%2C%20if%20it%20does%20then%20it%20would%20say%20'_____%20already%20exists'%20and%20stops%20the%20script%20going%20any%20further%20as%20it's%20currently%20doing%20for%20any%20users%20whether%20or%20not%20they%20exist%20-%20below.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat's%20the%20correct%20commands%20I%20should%20be%20using%20as%20below%20so%20it%20will%20search%20AD%20for%20that%20user%20and%20if%20they%20don't%20exist%20it%20would%20say%20'User%20doesn't%20exist'%20and%20continue%20with%20the%20rest%20of%20the%20script%20which%20then%20asks%20for%20the%20user's%20dept%20and%20phone%20number%20etc%2C%20as%20currently%20it's%20just%20saying%20all%20the%20user's%20already%20exist%20and%20stops%20the%20script%2C%20I've%20tried%20various%20recommended%20commands%20from%20other%20forums.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Jord9857_1-1632586783484.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312876i03B98A0AB8EDC3B3%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Jord9857_1-1632586783484.png%22%20alt%3D%22Jord9857_1-1632586783484.png%22%20%2F%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Jord9857_0-1632586701122.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312877i7C9E7D5E2E5D7385%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Jord9857_0-1632586701122.png%22%20alt%3D%22Jord9857_0-1632586701122.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2784891%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2785548%22%20slang%3D%22en-US%22%3ERe%3A%20Check%20if%20user%20already%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2785548%22%20slang%3D%22en-US%22%3E%3CP%3EIt's%20difficult%20to%20read%20your%20code%2C%20since%20the%20picture%20loads%20very%20small%20on%20my%20browser.%20Next%20time%20I%20suggest%20posting%20the%20code%20itself%20instead%20of%20screenshots.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20not%20a%20fan%20of%20scripts%20written%20for%20other%20people%20to%20type%20in.%20Read-host%20is%20kinda%20neat%20the%20first%20day%20you%20discover%20it%2C%20but%20I%20think%20you'll%20end%20up%20discovering%20that%20this%20approach%20is%20far%20too%20fragile.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20aside%2C%20checking%20to%20see%20if%20an%20account%20exists%20or%20not%20is%20a%20pretty%20common%20requirement.%20There%20are%20lots%20of%20ways%20to%20do%20it%2C%20but%20in%20general%2C%20you%20try%20to%20get%20the%20account%20in%20question%2C%20and%20if%20you%20couldn't%2C%20you%20know%20it%20doesn't%20exist!%20You'll%20need%20to%20make%20sure%20%22couldn't%22%20isn't%20simply%20because%20of%20environmental%20issues%2C%20like%20the%20network%20being%20down%2C%20etc.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETwo%20samples%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23Example%201%0A%0A%24AdUser%20%3D%20Get-ADUser%20-Filter%20%7BSamAccountName%20-eq%20%24SamAccountName%7D%0A%0Aif%20(%24AdUser%20-eq%20%24null)%20%7B%3CUSERDOESNTEXISTCODE%3E%7D%0Aelse%20%20%7B%3CUSEREXISTSCODE%3E%7D%0A%0A%0A%23Example%202%0A%0A%24AdUser%20%3D%20Get-ADUser%20-Filter%20%7BSamAccountName%20-eq%20%24SamAccountName%7D%0A%0Aif%20(%24AdUser%20-is%20'Microsoft.ActiveDirectory.Management.ADUser')%20%7B%3CUSEREXISTSCODE%3E%7D%0Aelse%20%20%7B%3CUSERDOESNTEXISTCODE%3E%7D%3C%2FUSERDOESNTEXISTCODE%3E%3C%2FUSEREXISTSCODE%3E%3C%2FUSEREXISTSCODE%3E%3C%2FUSERDOESNTEXISTCODE%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EBe%20aware%20that%20I'm%20assuming%20the%20match%20is%20simply%20on%20the%20samAccountName%20attribute.%20If%20you%20want%20to%20search%20more%20broadly%20(but%20maybe%20get%20multiple%20responses)%2C%20you%20could%20try%20other%20attributes%2C%20or%20the%20%3CA%20title%3D%22-anr%22%20href%3D%22https%3A%2F%2Fsocial.technet.microsoft.com%2Fwiki%2Fcontents%2Farticles%2F22653.active-directory-ambiguous-name-resolution.aspx%23PowerShell_Example_with_Get-ADUser%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Eambiguous%20name%20resolution%20feature%3C%2FA%3E.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi there,

 

I'm trying to create a script which basically requires the user's input, so they'll need to enter their first name & last name and AD will check if that AD account already exists for the user or not, if it does then it would say '_____ already exists' and stops the script going any further as it's currently doing for any users whether or not they exist - below.

 

What's the correct commands I should be using as below so it will search AD for that user and if they don't exist it would say 'User doesn't exist' and continue with the rest of the script which then asks for the user's dept and phone number etc, as currently it's just saying all the user's already exist and stops the script, I've tried various recommended commands from other forums.

 

Thank you in advance

 

Jord9857_1-1632586783484.pngJord9857_0-1632586701122.png

 

 

1 Reply

It's difficult to read your code, since the picture loads very small on my browser. Next time I suggest posting the code itself instead of screenshots.

 

I'm not a fan of scripts written for other people to type in. Read-host is kinda neat the first day you discover it, but I think you'll end up discovering that this approach is far too fragile.

 

That aside, checking to see if an account exists or not is a pretty common requirement. There are lots of ways to do it, but in general, you try to get the account in question, and if you couldn't, you know it doesn't exist! You'll need to make sure "couldn't" isn't simply because of environmental issues, like the network being down, etc.

 

Two samples:

#Example 1

$AdUser = Get-ADUser -Filter {SamAccountName -eq $SamAccountName}

if ($AdUser -eq $null) {<UserDoesntExistCode>}
else  {<UserExistsCode>}


#Example 2

$AdUser = Get-ADUser -Filter {SamAccountName -eq $SamAccountName}

if ($AdUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {<UserExistsCode>}
else  {<UserDoesntExistCode>}

Be aware that I'm assuming the match is simply on the samAccountName attribute. If you want to search more broadly (but maybe get multiple responses), you could try other attributes, or the ambiguous name resolution feature.