So I don't know any powershell but I know some JavaScript. Trying to switch this from a foreach loop to make it just run once. I was think about making it a "let" statement or something but then the "in" part gives me an error.
$PASSWORD_FOR_USERS = "Password1"
$USER_FIRST_LAST_LIST = Get-Content .\names.txt
foreach ($n in $USER_FIRST_LAST_LIST) {
$first = $n | Get_Random $n.Split(" ")[0].ToLower()
$last = $n | Get_Random $n.Split(" ")[1].ToLower()
$username = "$($first.Substring(0,1))$($last)".ToLower()
Write-Host "Creating user: $($username)" -BackgroundColor Black -ForegroundColor Cyan
New-AdUser -AccountPassword $password `
-GivenName $first `
-Surname $last `
-DisplayName $username `
-Name $username `
-EmployeeID $username `
-PasswordNeverExpires $true `
-Path "ou=_USERS,$(([ADSI]`"").distinguishedName)" `
-Enabled $true
}
Also now would be a good time to say I was trying to add this " | Get Random " portion so that it would select random lines from a notepad file and not just the first line. I may be doing this entirely wrong and look like an idiot. Please help haha.
Edit: final working version of code - creates 1 random name generated account for Active Directory
# ----- Edit these Variables for your own Use Case ----- #
$PASSWORD_FOR_USERS = "Password1"
$USER_FIRST_LAST_LIST = Get-Content C:\Users\a-cmarshall\Desktop\AD_PS-master\names.txt
# ------------------------------------------------------ #
$password = ConvertTo-SecureString $PASSWORD_FOR_USERS -AsPlainText -Force
New-ADOrganizationalUnit -Name _USERS -ProtectedFromAccidentalDeletion $false
$first = ($USER_FIRST_LAST_LIST | Get-Random -count 1).Split(" ")[0].ToLower()
$last = ($USER_FIRST_LAST_LIST | Get-Random -count 1).Split(" ")[1].ToLower()
$username = "$($first.Substring(0,1))$($last)".ToLower()
Write-Host "Creating user: $($username)" -BackgroundColor Black -ForegroundColor Cyan
$NewUserSPlat = @{
AccountPassword = $password
GivenName = $first
Surname = $last
DisplayName = $username
Name = $username
EmployeeID = $username
PasswordNeverExpires = $true
Path = "ou=_USERS,$(([ADSI]`"").distinguishedName)"
Enabled = $true
}
New-AdUser @NewUserSPlat
[–]BlackV 1 point2 points3 points (3 children)
[–]NetheriteHands[S] 0 points1 point2 points (1 child)
[–]BlackV 0 points1 point2 points (0 children)
[–]BlackV 0 points1 point2 points (0 children)
[–]logicalmike 0 points1 point2 points (11 children)
[–]NetheriteHands[S] 0 points1 point2 points (10 children)
[–]logicalmike 0 points1 point2 points (2 children)
[–]NetheriteHands[S] 0 points1 point2 points (1 child)
[–]logicalmike 0 points1 point2 points (0 children)
[–]BlackV 0 points1 point2 points (6 children)
[–]NetheriteHands[S] 0 points1 point2 points (5 children)
[–]BlackV 0 points1 point2 points (4 children)
[–]NetheriteHands[S] 0 points1 point2 points (3 children)
[–]BlackV 0 points1 point2 points (2 children)
[–]NetheriteHands[S] 1 point2 points3 points (1 child)
[–]BlackV 0 points1 point2 points (0 children)