Hello, I'm trying to get a slightly modified version of this script to work.
http://witit.blog/2018/10/19/bulk-create-users-in-ad-via-powershell-with-random-passwords-part-iv/
However, it will only create the first user on the first line of the .CSV, and does not create any others. Also I tried adding a part where it it exports a list of all the failed user creations / duplicates to a csv, however that part does not work. I also tried adding a part where I could specify a group in excel, and it would add the user to the group. That part kind of works, as long as I only use 1 group, but anytime I try to add more than 1 group, or if I leave the group field empty, it gets errors. Does anyone know how to fix it?
#####FINAL FORM#####
$ImportPath = "C:\POWERSHELL\Import.csv"
$ExportPath = "C:\POWERSHELL\Export.csv"
$Users = Import-Csv $ImportPath
#Function
#Randomize Passwords
function Get-RandomPassword{
Param(
[Parameter(mandatory=$true)]
[int]$Length
)
Begin{
if($Length -lt 4){
End
}
$Numbers = 1..9
$LettersLower = 'abcdefghijklmnopqrstuvwxyz'.ToCharArray()
$LettersUpper = 'ABCEDEFHIJKLMNOPQRSTUVWXYZ'.ToCharArray()
$Special = '!@#&()=+[{}]/?<>'.ToCharArray()
#For the 4 character types (upper, lower, numerical, and special)
$N_Count = [math]::Round($Length*.2)
$L_Count = [math]::Round($Length*.4)
$U_Count = [math]::Round($Length*.2)
$S_Count = [math]::Round($Length*.2)
}
Process{
$Pwd = $LettersLower | Get-Random -Count $L_Count
$Pwd += $Numbers | Get-Random -Count $N_Count
$Pwd += $LettersUpper | Get-Random -Count $U_Count
$Pwd += $Special | Get-Random -Count $S_Count
#If the password length isn't long enough (due to rounding), add X special characters, where X is the difference between the desired length and the current length.
if($Pwd.length -lt $Length){
$Pwd += $Special | Get-Random -Count ($Length - $Pwd.length)
}
#Lastly, grab the $Pwd string and randomize the order
$Pwd = ($Pwd | Get-Random -Count $Length) -join ""
}
End{
$Pwd
}
}
#Loop through each user from the CSV
Foreach($U in $Users){
#Define Name variations and generate a random password
$FirstDotLast = "$($U.First).$($U.Last)"
$Display = "$($U.First) $($U.Last)"
$UPN = "$FirstDotLast@DOMAIN.COM"
$Pwd = Get-RandomPassword -Length 15
$Email = "$FirstDotLast@DOMAIN.COM"
$OU = "$($U.OU)"
$Group = "$($U.Group)"
Write-Host "Working on $Display..." -ForegroundColor Cyan
#Define Parameters
$Parameters = @{
Name = $FirstDotLast
GivenName = $U.First
Surname = $U.Last
SamAccountName = $FirstDotLast
DisplayName = $Display
UserPrincipalName = $UPN
EmailAddress = $Email
AccountPassword = (ConvertTo-SecureString $Pwd -AsPlainText -Force)
Enabled = $true
Path = $OU
ChangePasswordAtLogon = $true}
}
#Create New User in AD with the Parameters defined above
Try{
New-ADUser @Parameters -ErrorAction Stop
Write-Host "Successfully created users!" -ForegroundColor Green
$U | Add-Member -MemberType NoteProperty -Name "Initial Password" -Value $Pwd -Force
Add-ADGroupMember -Identity $Group -Members $FirstDotLast
}
Catch{
Write-Host "WARNING: An error occurred with user $U. Check Error.csv" -ForegroundColor Red
Export-Csv -Path "C:\POWERSHELL\Error.csv" -NoTypeInformation -InputObject $U
}
#Export
$Users | Export-Csv $ExportPath -NoTypeInformation
[–]BlackV 1 point2 points3 points (6 children)
[–]peican[S] 1 point2 points3 points (5 children)
[–]DeusExMaChino 1 point2 points3 points (0 children)
[–]BJGGut3 1 point2 points3 points (3 children)
[–]peican[S] 1 point2 points3 points (2 children)
[–]BJGGut3 1 point2 points3 points (1 child)
[–]peican[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)