0

I am trying to add multiple users, by writing the script under multiple times with different user names. Is it possible to repeat this line of code adding multiple user names in a different way? Which will be more effective and easier rather than importing csv file.

New-ADUser -Name "Test Eksamen" -GivenName "Test" -Surname Eksamen -SamAccountName Olf -UserPrincipalName [email protected] -path "OU=powershell, DC=test, DC=local"
5
  • You can to add them eg. into array / arraylist and just iterate over it and take item on the position, as well as it is possible in "every" programming language :) Commented Dec 15, 2017 at 8:32
  • 1
    Can you explain why don't you want to use a csv as it's the easiest way to complete the task you're doing.. Commented Dec 15, 2017 at 8:34
  • Bellow in answer post :) @JamesC. Commented Dec 15, 2017 at 8:53
  • Was aimed at OP not you @xxxvodnikxxx Commented Dec 15, 2017 at 8:57
  • @JamesC. Ah, ok, doesn't matter :D Commented Dec 15, 2017 at 8:58

3 Answers 3

3

sorry I can't comment because i don't have enough reputation. It's a bit unclear to me what's wrong with csv importing and what do you mean by "easier way". In my opinion importing csv is the most convenient way.

You could use arrays with names and surnames.

    $names = @("Neil","Albert", "Nikola")
    $surnames = @("Degrasse", "Einstein", "Tesla")

foreach ($user in (0.. ($names.Length -1))){
    $user_name = $names[$user]
    $user_surname = $surnames[$user]
    New-ADUser -Name "$user_name $user_surname" -GivenName $user_name -Surname $user_surname -SamAccountName "$user_name.$user_surname" -UserPrincipalName "[email protected]" -Path "OU=powershell, DC=test, DC=local"
    }

Once again i'm sorry if it's not what you were looking for, I just can't comment because of lack of reputation.

Sign up to request clarification or add additional context in comments.

Comments

0

As I mentioned in the comment, you can to add them into some array or arraylist, whatever, something like bellow.

Btw I guess csv is probably the most comfort way how to do it.

Notice- because I dont know how to make some objects in the single PS script, I have used standard object with custom properties

#your record New-ADUser -Name "Test Eksamen" -GivenName "Test" -Surname Eksamen -SamAccountName Olf -UserPrincipalName [email protected] -path "OU=powershell, DC=test, DC=local"


 #custom object definition-example of 1 member
 $member= New-Object System.Object
 $member | Add-Member -type NoteProperty -name cName -value "Test Eksamen"
 $member | Add-Member -type NoteProperty -name cGivenName -value "Test"
 $member | Add-Member -type NoteProperty -name cSurname -value "Eksamen"
 $member | Add-Member -type NoteProperty -name cSamAccountName -value "Olf"
 $member | Add-Member -type NoteProperty -name cUserPrincipalName -value "[email protected]"
 $member | Add-Member -type NoteProperty -name cpath -value "OU=powershell, DC=test, DC=local"

#add into some list
 $memberList = New-Object System.Collections.ArrayList
 $memberList.Add($member)

#iteration and adding each 
  foreach($actMember in $memberList){
    New-ADUser -Name $actMember.cName  -GivenName $actMember.cGivenName -Surname $actMember.cName -SamAccountName $actMember.cSurname -UserPrincipalName $actMember.cUserPrincipalName -path $actMember.cpath
   }

Other way can be eg. to store each data separately, like in example from @Tesla Great

$names = @("Neil","Albert", "Nikola")
#should be exactly the same
#$names = "Neil","Albert", "Nikola"
$givenNames= @("Degrasse", "Einstein", "Tesla")
#..... other params

#and iterate 
For ($i=0; $i -le ($names.Length -1); $i++) {
    New-ADUser -Name $names[i] -GivenName $givenNames[i] ... etc
}

The second solution is let me say little messy, because there is no data integrity (you can very simply eg. swap some values, or make some missing, etc.) between these variables, but first one is seriously longer code

Comments

0

also you can randomly create user as user1.2..3 etc

$names=1..1000
$x="user"
Foreach($name in $names){
$nm=$x + "$name"
$Password = (ConvertTo-SecureString -AsPlainText 'P@ssw0rd@123' -Force)
New-ADUser -GivenName $x -Surname $name -Name $nm -Enabled $true -AccountPassword $password -SamAccountName $nm -WhatIf
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.