Reputation:
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"
Upvotes: 0
Views: 395
Reputation: 1016
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
}
Upvotes: 0
Reputation: 1277
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
Upvotes: 0
Reputation: 114
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.
Upvotes: 3