Creating Users with a CSV

%3CLINGO-SUB%20id%3D%22lingo-sub-2910502%22%20slang%3D%22en-US%22%3ECreating%20Users%20with%20a%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2910502%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20trying%20to%20create%20a%20script%20which%20will%20pull%20user's%20info%20from%20a%20.csv%2C%20check%20the%20user's%20DisplayName%20with%20AD%20to%20make%20sure%20a%20user%20with%20that%20DisplayName%20doesn't%20already%20exist%2C%20does%20exact%20same%20with%20user's%20SamAccountName%20after.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOnce%20done%20these%20checks%2C%20if%20the%20user's%20DisplayName%20%26amp%3B%20SamAccountName%20doesn't%20already%20exist%20in%20AD%20it%20will%20then%20create%20the%20users%20with%20the%20info%20from%20the%20.csv%20then%20it%20will%20check%20if%20that%20user%20has%20been%20successfully%20created%20by%20checking%20-%20else%2C%20it'll%20say%20it%20was%20unsuccessful.%20I'm%20doing%20something%20similar%20like%20this%20with%20importing%20computers%20into%20AD%20too%20but%20same%20issue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20script%20has%20worked%20for%20creating%20single%20user's%20but%20when%20it%20comes%20to%20creating%20multiple%20user's%2C%20it%20gets%20itself%20in%20a%20muddle.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20done%20a%20lot%20of%20research%20and%20testing%20e.g.%20trying%20to%20use%20the%20'break'%20but%20that%20causes%20the%20whole%20script%20to%20stop%20after%20the%20first%20user%20is%20detected%20as%20it's%20exists%2C%20trying%20to%20use%20'continue'%20but%20it%20didn't%20make%20much%20difference%2C%20trying%20to%20bracket%20bits%20off%2C%20tried%20doing%20if%20'false'%20or%20if%20'true'%20after%20the%20%24%3F%20wildcard%2C%20trying%20different%20operators%20and%20wildcards%2C%20using%20if-not%20and%20else%20etc%20but%20just%20can't%20seem%20to%20function%20properly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20maybe%20a%20lot%20of%20questioning%20why%20I've%20done%20certain%20commands%20instead%20of%20other%20commands%2C%20a%20lot%20of%20this%20script%20has%20been%20me%20researching%20and%20testing%20on%20my%20lab%20(none%20of%20this%20info%20is%20genuine%20user's%20info%2C%20it's%20just%20self%20learning%20on%20my%20lab)%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20open%20to%20any%20command%20recommendations%20or%20suggestions%20as%20I%20mentioned%2C%20this%20is%20purely%20for%20self%20learning%20and%20lab%20purposes.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Start%20Commands%0AWrite-Host%20%22STARTED%20SCRIPT%60r%60n%22%0A%0A%23%20Import%20Active%20Directory%20Module%20For%20Running%20AD%20Cmdlets%0AImport-Module%20ActiveDirectory%0A%0A%23%20Store%20The%20Data%20From%20ADUsers.csv%20in%20THE%20%24ADUsers%20variable%0A%24filepath%20%3D%20Import-Csv%20-Path%20C%3A%5CUsers%5CAdministrator%5CDesktop%5Cusers1.csv%0A%0A%23%20Loop%20through%20each%20row%20containing%20user%20details%20in%20the%20csv%20file%0AForEach%20(%24user%20in%20(%24filepath))%0A%7B%0A%0A%23%20Check%20if%20User(s)%20display%20name%20already%20exists%0A%24ADUser%20%3D%20Get-AdUser%20-Filter%20%7B%24displayname%20-eq%20%24User.DisplayName%7D%0Aif%20(%24ADUser%20-is%20'Microsoft.ActiveDirectory.Management.ADUser')%20%7BWrite-Host%20%22%24displayname%20already%20exists%22%20-ForegroundColor%20Red%7D%0A%0A%23%20Check%20if%20User(s)%20username%20already%20exists%0A%24ADUser%20%3D%20Get-AdUser%20-Filter%20%7B%24SamAccountName%20-eq%20%24User.SamAccountName%7D%0Aif%20(%24ADUser%20-is%20'Microsoft.ActiveDirectory.Management.ADUser')%20%7BWrite-Host%20%22%24SamAccountName%20already%20exists%22%20-ForegroundColor%20Red%7D%0A%0A%0A%20%20%20%20%23%20Read%20user%20data%20from%20each%20field%20in%20each%20row%20and%20assign%20the%20data%20to%20a%20variable%20as%20below%0A%20%20%20%20%24displayname%20%3D%20%24User.'firstname'%20%2B%20%22%20%22%20%2B%20%24User.'lastname'%0A%20%20%20%20%24firstname%20%3D%20%24User.'firstname'%0A%20%20%20%20%24lastname%20%3D%20%24User.'lastname'%0A%20%20%20%20%24jobtitle%20%3D%20%24User.'jobtitle'%0A%20%20%20%20%24telephone%20%3D%20%24User.'telephone'%0A%20%20%20%20%24department%20%3D%20%24User.'department'%0A%20%20%20%20%24UPN%20%3D%20%24User.'email'%0A%20%20%20%20%24SamAccountName%20%3D%20%24User.'username'%0A%20%20%20%20%24EmailAddress%20%3D%20%24User.'email'%0A%20%20%20%20%24Password%20%3D%20%24User.'password'%0A%20%20%20%20%24OU%20%3D%20%24User.'OU'%0A%20%20%20%20%0A%20%20%20%20%23%20Create%20New%20AD%20Users%0A%20%20%20%20New-ADUser%20-Name%20%22%24displayname%22%20-DisplayName%20%22%24displayname%22%20-GivenName%20%22%24firstname%22%20-Surname%20%22%24lastname%22%20-Title%20%22%24jobtitle%22%20-OfficePhone%20%22%24telephone%22%20-Department%20%22%24department%22%20-UserPrincipalName%20%22%24UPN%22%20-SamAccountName%20%22%24SamAccountName%22%20-EmailAddress%20%22%24EmailAddress%22%20-AccountPassword%20(ConvertTo-SecureString%20%24password%20-AsPlainText%20-Force)%20-Enabled%20%24true%20-Path%20%22%24OU%22%20-ChangePasswordAtLogon%20%24true%20-PasswordNeverExpires%20%24false%0A%20%20%20%20%0A%20%20%20%20%23%20Confirm%20Commands%0A%20%20%20%20%24%3F%20%7C%20out-null%0A%20%20%20%20%0A%20%20%20%20%24ADUser%20%3D%20Get-ADUser%20-Filter%20%7BDisplayName%20-eq%20%24displayname%7D%0A%20%20%20%20if%20(%24ADUser%20-is%20'Microsoft.ActiveDirectory.Management.ADUser')%20%7BWrite-Host%20%22%24displayname%20account%20was%20successfully%20created%22%20-ForegroundColor%20Green%7D%0A%20%20%20%20%0A%20%20%20%20else%20%7BWrite-Host%20%22%24displayname%20account%20was%20unsuccessfully%20created%22%20-ForegroundColor%20Red%7D%0A%0A%20%20%20%20%24ErrorActionPreference%20%3D%20'SilentlyContinue'%0A%20%20%20%20%7D%0A%0A%23%20Finish%20Commands%0AWrite-Host%20%22FINISHED%20SCRIPT%60r%60n%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20the%20CSV%20it's%20pulling%20the%20info%20from.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3Efirstname%20lastname%20jobtitle%20telephone%20department%20username%20email%20password%20OU%0AFred%20Jones%20Maths%20Teacher%20987654321%20Maths%20Fred.Jones%20Fred.Jones%40Jord.Local%20Hello2021!%20OU%3DTeaching%20Staff%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%0AFred%20Try%20Administrator%20123456789%20Admin%20Fred.Try%20Fred.Try%40Jord.Local%20Hello2021!%20OU%3DAdmin%20Staff%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%0ARen%20Jones%20English%20Teacher%20134258679%20English%20Ren.Jones%20Ren.Jones%40Jord.Local%20Hello2021!%20OU%3DCustomers%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%0ASophie%20Hop%20Lanuages%20Teacher%20174392834%20Languages%20Sophie.Hop%20Sophie.Hop%40Jord.Local%20Hello2021!%20OU%3DOther%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20the%20output%20of%20the%20script%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EPS%20C%3A%5CUsers%5CAdministrator%26gt%3B%20%5C%5CFREENAS%5CNetwork%5CPowerShell%5CUsers%5CUser%20Creation%5CAutomatically%5Ctest.ps1%0ASTARTED%20SCRIPT%0A%0ASophie%20Hop%20already%20exists%0ASophie.Hop%20already%20exists%0AFred%20Jones%20account%20was%20successfully%20created%0AFred%20Jones%20already%20exists%0AFred.Jones%20already%20exists%0AFred%20Try%20account%20was%20successfully%20created%0AFred%20Try%20already%20exists%0AFred.Try%20already%20exists%0ARen%20Jones%20account%20was%20successfully%20created%0ARen%20Jones%20already%20exists%0ARen.Jones%20already%20exists%0ASophie%20Hop%20account%20was%20successfully%20created%0AFINISHED%20SCRIPT%0A%0A%0APS%20C%3A%5CUsers%5CAdministrator%26gt%3B%20%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eor%20even%20taking%20out%20this%20bit%20of%20script%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24ADUser%20%3D%20Get-ADUser%20-Filter%20%7BDisplayName%20-eq%20%24displayname%7D%0A%20%20%20%20if%20(%24ADUser%20-is%20'Microsoft.ActiveDirectory.Management.ADUser')%20%7BWrite-Host%20%22%24displayname%20account%20was%20successfully%20created%22%20-ForegroundColor%20Green%7D%0A%20%20%20%20%0A%20%20%20%20else%20%7BWrite-Host%20%22%24displayname%20account%20was%20unsuccessfully%20created%22%20-ForegroundColor%20Red%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eit%20still%20gets%20in%20a%20mess%20with%20this%20output%20(even%20though%20the%20AD%20account%20don't%20exist%20beforehand%2C%20they%20do%20after%20the%20script%20is%20ran%20though)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3EPS%20C%3A%5CUsers%5CAdministrator%26gt%3B%20%5C%5CFREENAS%5CNetwork%5CPowerShell%5CUsers%5CUser%20Creation%5CAutomatically%5Ctest.ps1%0ASTARTED%20SCRIPT%0A%0ASophie%20Hop%20already%20exists%0ASophie.Hop%20already%20exists%0AFred%20Jones%20already%20exists%0AFred.Jones%20already%20exists%0AFred%20Try%20already%20exists%0AFred.Try%20already%20exists%0ARen%20Jones%20already%20exists%0ARen.Jones%20already%20exists%0AFINISHED%20SCRIPT%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%2C%20suggestions%20or%20recommendations%20are%20very%26nbsp%3Bappreciated%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2910502%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2913436%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20Users%20with%20a%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2913436%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1166463%22%20target%3D%22_blank%22%3E%40Jord9857%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3CBR%20%2F%3EYour%20script%20is%20correct%20but%20you%20are%20miss%20something%3A%3CBR%20%2F%3EIn%20your%20csv%20file%20you%20doesn't%20have%20DisplayName%20or%20SamAccountName%20value%2C%20so%20you%20can't%20check%20if%20user%20exist%20or%20not%20%2C%20see%20line%2015%20of%20your%20script%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24ADUser%20%3D%20Get-AdUser%20-Filter%20%7B%24displayname%20-eq%20%24User.DisplayName%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20you%20have%20the%20choice%2C%20or%20you%20adding%20this%20columns%20in%20the%20csv%20with%20the%20correct%20value%20or%20you%20check%20if%20user%20exist%20with%20other%20parameters%2C%20for%20example%20firstname%20or%20lastname%20combined%20together.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20this%20help%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2916737%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20Users%20with%20a%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2916737%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1201851%22%20target%3D%22_blank%22%3E%40AharonBensadoun%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20response.%3CBR%20%2F%3EHowever%20even%20making%20this%20adjustment%20to%20the%20.csv%2C%20it%20still%20doesn't%20work%20properly.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Efirstname%20lastname%20displayname%20jobtitle%20telephone%20department%20username%20SamAccountName%20email%20password%20OU%0AFred%20Jones%20Fred%20Jones%20Maths%20Teacher%20987654321%20Maths%20Fred.Jones%20Fred.Jones%20Fred.Jones%40Jord.Local%20Hello2021!%20OU%3DTeaching%20Staff%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%0AFred%20Try%20Fred%20Try%20Administrator%20123456789%20Admin%20Fred.Try%20Fred.Try%20Fred.Try%40Jord.Local%20Hello2021!%20OU%3DAdmin%20Staff%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%0ARen%20Jones%20Ren%20Jones%20English%20Teacher%20134258679%20English%20Ren.Jones%20Ren.Jones%20Ren.Jones%40Jord.Local%20Hello2021!%20OU%3DCustomers%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%0ASophie%20Hop%20Sophie%20Hop%20Lanuages%20Teacher%20174392834%20Languages%20Sophie.Hop%20Sophie.Hop%20Sophie.Hop%40Jord.Local%20Hello2021!%20OU%3DOther%2COU%3DUsers%2COU%3DTest%2CDC%3DJord%2CDC%3DLocal%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20the%20first%20output%20(user's%20don't%20exist)%20-%20it%20works%20completely%20fine%20for%20the%20user%20Sophie%20Hop%20but%20same%20issue%20with%20the%20other%20user's%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ESTARTED%20SCRIPT%0A%0AGet-AdUser%20%3A%20Variable%3A%20'displayname'%20found%20in%20expression%3A%20%24displayname%20is%20not%20defined.%0AAt%20test.ps1%3A15%20char%3A11%0A%2B%20%24ADUser%20%3D%20Get-AdUser%20-Filter%20%7BDisplayName%20-eq%20%24displayname%7D%0A%2B%20%20%20%20%20%20%20%20%20%20%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20InvalidArgument%3A%20(%3A)%20%5BGet-ADUser%5D%2C%20ArgumentException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20ActiveDirectoryCmdlet%3ASystem.ArgumentException%2CMicrosoft.ActiveDirectory.Management.Commands.GetADUser%0A%20%0AGet-AdUser%20%3A%20Variable%3A%20'SamAccountName'%20found%20in%20expression%3A%20%24SamAccountName%20is%20not%20defined.%0AAt%20test.ps1%3A19%20char%3A11%0A%2B%20%24ADUser%20%3D%20Get-AdUser%20-Filter%20%7BSamAccountName%20-eq%20%24SamAccountName%7D%0A%2B%20%20%20%20%20%20%20%20%20%20%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20InvalidArgument%3A%20(%3A)%20%5BGet-ADUser%5D%2C%20ArgumentException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20ActiveDirectoryCmdlet%3ASystem.ArgumentException%2CMicrosoft.ActiveDirectory.Management.Commands.GetADUser%0A%20%0AFred%20Jones%20account%20was%20successfully%20created%0AFred%20Jones%20already%20exists%0AFred.Jones%20already%20exists%0AFred%20Try%20account%20was%20successfully%20created%0AFred%20Try%20already%20exists%0AFred.Try%20already%20exists%0ARen%20Jones%20account%20was%20successfully%20created%0ARen%20Jones%20already%20exists%0ARen.Jones%20already%20exists%0ASophie%20Hop%20account%20was%20successfully%20created%0AFINISHED%20SCRIPT%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20the%202nd%20output%20(user's%20already%20exist)%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3ESTARTED%20SCRIPT%0A%0ASophie%20Hop%20already%20exists%0ASophie.Hop%20already%20exists%0AFred%20Jones%20account%20was%20successfully%20created%0AFred%20Jones%20already%20exists%0AFred.Jones%20already%20exists%0AFred%20Try%20account%20was%20successfully%20created%0AFred%20Try%20already%20exists%0AFred.Try%20already%20exists%0ARen%20Jones%20account%20was%20successfully%20created%0ARen%20Jones%20already%20exists%0ARen.Jones%20already%20exists%0ASophie%20Hop%20account%20was%20successfully%20created%0AFINISHED%20SCRIPT%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2929979%22%20slang%3D%22en-US%22%3ERe%3A%20Creating%20Users%20with%20a%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2929979%22%20slang%3D%22en-US%22%3EHi%2C%3CBR%20%2F%3EYou%20need%20to%20look%20at%20your%20logic%20a%20bit%20closer.%20You%20have%20IF%20Statements%20but%20they%20are%20not%20nested%20or%3CBR%20%2F%3Eare%20using%20AND%20comparison%20to%20have%20both%20components%20match.%20Also%20you%20are%20not%20throwing%20a%20variable%20that%20says%20the%20user%20exists%20to%20provide%20a%20logic%20check%20if%20you%20actually%20need%20to%20create%20the%20account.%20Right%20now%20it%20runs%20each%20if%20statement%20then%20runs%20the%20creation%20statement.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20find%20it%20may%20help%20sometimes%20to%20write%20down%20in%20a%20document%20the%20logic%20process%20before%20tackling%20the%20coding.%3C%2FLINGO-BODY%3E
Occasional Contributor

Hi all,

 

I'm trying to create a script which will pull user's info from a .csv, check the user's DisplayName with AD to make sure a user with that DisplayName doesn't already exist, does exact same with user's SamAccountName after.

 

Once done these checks, if the user's DisplayName & SamAccountName doesn't already exist in AD it will then create the users with the info from the .csv then it will check if that user has been successfully created by checking - else, it'll say it was unsuccessful. I'm doing something similar like this with importing computers into AD too but same issue.

 

This script has worked for creating single user's but when it comes to creating multiple user's, it gets itself in a muddle.

 

I've done a lot of research and testing e.g. trying to use the 'break' but that causes the whole script to stop after the first user is detected as it's exists, trying to use 'continue' but it didn't make much difference, trying to bracket bits off, tried doing if 'false' or if 'true' after the $? wildcard, trying different operators and wildcards, using if-not and else etc but just can't seem to function properly.

 

There maybe a lot of questioning why I've done certain commands instead of other commands, a lot of this script has been me researching and testing on my lab (none of this info is genuine user's info, it's just self learning on my lab) 

 

I'm open to any command recommendations or suggestions as I mentioned, this is purely for self learning and lab purposes.

 

 

 

# Start Commands
Write-Host "STARTED SCRIPT`r`n"

# Import Active Directory Module For Running AD Cmdlets
Import-Module ActiveDirectory

# Store The Data From ADUsers.csv in THE $ADUsers variable
$filepath = Import-Csv -Path C:\Users\Administrator\Desktop\users1.csv

# Loop through each row containing user details in the csv file
ForEach ($user in ($filepath))
{

# Check if User(s) display name already exists
$ADUser = Get-AdUser -Filter {$displayname -eq $User.DisplayName}
if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {Write-Host "$displayname already exists" -ForegroundColor Red}

# Check if User(s) username already exists
$ADUser = Get-AdUser -Filter {$SamAccountName -eq $User.SamAccountName}
if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {Write-Host "$SamAccountName already exists" -ForegroundColor Red}


    # Read user data from each field in each row and assign the data to a variable as below
    $displayname = $User.'firstname' + " " + $User.'lastname'
    $firstname = $User.'firstname'
    $lastname = $User.'lastname'
    $jobtitle = $User.'jobtitle'
    $telephone = $User.'telephone'
    $department = $User.'department'
    $UPN = $User.'email'
    $SamAccountName = $User.'username'
    $EmailAddress = $User.'email'
    $Password = $User.'password'
    $OU = $User.'OU'
    
    # Create New AD Users
    New-ADUser -Name "$displayname" -DisplayName "$displayname" -GivenName "$firstname" -Surname "$lastname" -Title "$jobtitle" -OfficePhone "$telephone" -Department "$department" -UserPrincipalName "$UPN" -SamAccountName "$SamAccountName" -EmailAddress "$EmailAddress" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $true -PasswordNeverExpires $false
    
    # Confirm Commands
    $? | out-null
    
    $ADUser = Get-ADUser -Filter {DisplayName -eq $displayname}
    if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {Write-Host "$displayname account was successfully created" -ForegroundColor Green}
    
    else {Write-Host "$displayname account was unsuccessfully created" -ForegroundColor Red}

    $ErrorActionPreference = 'SilentlyContinue'
    }

# Finish Commands
Write-Host "FINISHED SCRIPT`r`n"

 

 

 

This is the CSV it's pulling the info from.

 

 

firstname	lastname	jobtitle	telephone	department	username	email	password	OU
Fred	Jones	Maths Teacher	987654321	Maths	Fred.Jones	Fred.Jones@Jord.Local	Hello2021!	OU=Teaching Staff,OU=Users,OU=Test,DC=Jord,DC=Local
Fred	Try	Administrator	123456789	Admin	Fred.Try	Fred.Try@Jord.Local	Hello2021!	OU=Admin Staff,OU=Users,OU=Test,DC=Jord,DC=Local
Ren	Jones	English Teacher	134258679	English	Ren.Jones	Ren.Jones@Jord.Local	Hello2021!	OU=Customers,OU=Users,OU=Test,DC=Jord,DC=Local
Sophie	Hop	Lanuages Teacher	174392834	Languages	Sophie.Hop	Sophie.Hop@Jord.Local	Hello2021!	OU=Other,OU=Users,OU=Test,DC=Jord,DC=Local

 

 

 

 

This is the output of the script

 

 

PS C:\Users\Administrator> \\FREENAS\Network\PowerShell\Users\User Creation\Automatically\test.ps1
STARTED SCRIPT

Sophie Hop already exists
Sophie.Hop already exists
Fred Jones account was successfully created
Fred Jones already exists
Fred.Jones already exists
Fred Try account was successfully created
Fred Try already exists
Fred.Try already exists
Ren Jones account was successfully created
Ren Jones already exists
Ren.Jones already exists
Sophie Hop account was successfully created
FINISHED SCRIPT


PS C:\Users\Administrator> 

 

 

 

or even taking out this bit of script

 

 

$ADUser = Get-ADUser -Filter {DisplayName -eq $displayname}
    if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {Write-Host "$displayname account was successfully created" -ForegroundColor Green}
    
    else {Write-Host "$displayname account was unsuccessfully created" -ForegroundColor Red}

 

 

it still gets in a mess with this output (even though the AD account don't exist beforehand, they do after the script is ran though)

 

 

PS C:\Users\Administrator> \\FREENAS\Network\PowerShell\Users\User Creation\Automatically\test.ps1
STARTED SCRIPT

Sophie Hop already exists
Sophie.Hop already exists
Fred Jones already exists
Fred.Jones already exists
Fred Try already exists
Fred.Try already exists
Ren Jones already exists
Ren.Jones already exists
FINISHED SCRIPT

 

 

Any help, suggestions or recommendations are very appreciated

8 Replies

@Jord9857 

 

Hi,
Your script is correct but you are miss something:
In your csv file you doesn't have DisplayName or SamAccountName value, so you can't check if user exist or not , see line 15 of your script:

$ADUser = Get-AdUser -Filter {$displayname -eq $User.DisplayName}

 

So you have the choice, or you adding this columns in the csv with the correct value or you check if user exist with other parameters, for example firstname or lastname combined together.

 

Hope this help

@AharonBensadoun 

 

Hi,

Thank you for your response.
However even making this adjustment to the .csv, it still doesn't work properly.

 

firstname	lastname	displayname	jobtitle	telephone	department	username	SamAccountName	email	password	OU
Fred	Jones	Fred Jones	Maths Teacher	987654321	Maths	Fred.Jones	Fred.Jones	Fred.Jones@Jord.Local	Hello2021!	OU=Teaching Staff,OU=Users,OU=Test,DC=Jord,DC=Local
Fred	Try	Fred Try	Administrator	123456789	Admin	Fred.Try	Fred.Try	Fred.Try@Jord.Local	Hello2021!	OU=Admin Staff,OU=Users,OU=Test,DC=Jord,DC=Local
Ren	Jones	Ren Jones	English Teacher	134258679	English	Ren.Jones	Ren.Jones	Ren.Jones@Jord.Local	Hello2021!	OU=Customers,OU=Users,OU=Test,DC=Jord,DC=Local
Sophie	Hop	Sophie Hop	Lanuages Teacher	174392834	Languages	Sophie.Hop	Sophie.Hop	Sophie.Hop@Jord.Local	Hello2021!	OU=Other,OU=Users,OU=Test,DC=Jord,DC=Local

 

This is the first output (user's don't exist) - it works completely fine for the user Sophie Hop but same issue with the other user's 

STARTED SCRIPT

Get-AdUser : Variable: 'displayname' found in expression: $displayname is not defined.
At test.ps1:15 char:11
+ $ADUser = Get-AdUser -Filter {DisplayName -eq $displayname}
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
Get-AdUser : Variable: 'SamAccountName' found in expression: $SamAccountName is not defined.
At test.ps1:19 char:11
+ $ADUser = Get-AdUser -Filter {SamAccountName -eq $SamAccountName}
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
Fred Jones account was successfully created
Fred Jones already exists
Fred.Jones already exists
Fred Try account was successfully created
Fred Try already exists
Fred.Try already exists
Ren Jones account was successfully created
Ren Jones already exists
Ren.Jones already exists
Sophie Hop account was successfully created
FINISHED SCRIPT

 

This is the 2nd output (user's already exist)

STARTED SCRIPT

Sophie Hop already exists
Sophie.Hop already exists
Fred Jones account was successfully created
Fred Jones already exists
Fred.Jones already exists
Fred Try account was successfully created
Fred Try already exists
Fred.Try already exists
Ren Jones account was successfully created
Ren Jones already exists
Ren.Jones already exists
Sophie Hop account was successfully created
FINISHED SCRIPT

 

Hi,
You need to look at your logic a bit closer. You have IF Statements but they are not nested or
are using AND comparison to have both components match. Also you are not throwing a variable that says the user exists to provide a logic check if you actually need to create the account. Right now it runs each if statement then runs the creation statement.

I find it may help sometimes to write down in a document the logic process before tackling the coding.
So what do I need to do/change please?
Hi @Jord9857,
Are you sur your csv is comma delimited ? Because I tested your script and it's working fine for me.

Hi @AharonBensadoun 

 

Yep, tried both CSV (Comma delimited) and CSV UTF-8 (Comma delimited) but still same issue :\

@Jord9857 

I've put some additional comments in your script but as I said before write out the logic on some paper and it will help you write the correct statements.  Right now you just have a loop that applies every action to each item that goes through the script.  The if statements should have an else statement that creates the list of users you are going to act against or you will just attempt to recreate all users each time.  The creation portion of the script should only loop through the users who don't exist properly, not every user.

 

# Start Commands
Write-Host "STARTED SCRIPT`r`n"

# Import Active Directory Module For Running AD Cmdlets
Import-Module ActiveDirectory

# Store The Data From ADUsers.csv in THE $ADUsers variable
$filepath = Import-Csv -Path C:\Users\Administrator\Desktop\users1.csv

# Loop through each row containing user details in the csv file
ForEach ($user in ($filepath))
    {

    # Check if User(s) display name already exists
    $ADUser = Get-AdUser -Filter {$displayname -eq $User.DisplayName}
    if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') 
        {
        Write-Host "$displayname already exists" -ForegroundColor Red
        }
#What are you going to do if the user doesn't exist? (else Statement should be here to put the Incorrects into an array to correct later right??

# Check if User(s) username already exists
    $ADUser = Get-AdUser -Filter {$SamAccountName -eq $User.SamAccountName}
    if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') 
        {
        Write-Host "$SamAccountName already exists" -ForegroundColor Red
        }
#What are you going to do if the username doesn't exist? (else Statement should be here to put the Incorrects into an array to correct later right??


# Should not this section only be done for users who didn't pass the above if statement? otherwise the following commands apply to all users in the file 

    # Read user data from each field in each row and assign the data to a variable as below
    $displayname = $User.'firstname' + " " + $User.'lastname'
    $firstname = $User.'firstname'
    $lastname = $User.'lastname'
    $jobtitle = $User.'jobtitle'
    $telephone = $User.'telephone'
    $department = $User.'department'
    $UPN = $User.'email'
    $SamAccountName = $User.'username'
    $EmailAddress = $User.'email'
    $Password = $User.'password'
    $OU = $User.'OU'
    
    # Create New AD Users
    New-ADUser -Name "$displayname" -DisplayName "$displayname" -GivenName "$firstname" -Surname "$lastname" -Title "$jobtitle" -OfficePhone "$telephone" -Department "$department" -UserPrincipalName "$UPN" -SamAccountName "$SamAccountName" -EmailAddress "$EmailAddress" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $true -PasswordNeverExpires $false
    
    # Confirm Commands
    $? | out-null
    
    $ADUser = Get-ADUser -Filter {DisplayName -eq $displayname}
    if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {Write-Host "$displayname account was successfully created" -ForegroundColor Green}
    
    else {Write-Host "$displayname account was unsuccessfully created" -ForegroundColor Red}

    $ErrorActionPreference = 'SilentlyContinue'
    }

# Finish Commands
Write-Host "FINISHED SCRIPT`r`n"

   

@Jord9857 

Hi Jord,

 

here's a script I did up a a while back (2019) for creating AD users if it's helpful CreateUsers.ps1 it has a CSV file for input NewUsertemplate.xlsx