powershell-批量创建AD用户

时间:2023-02-19 17:56:54
#批量创建AD用户并使用随机密码,将账户密码输出
$users = Get-Content -Path "C:\Users\Username\Documents\userlist.txt"
foreach ($user in $users) {
$FirstInitial = ($user.Substring(0,1))
$LastName = ($user.Substring(1))
$UPN = $FirstInitial + $LastName + "@yourdomain.com"
$DisplayName = $user
$Name = $user
$Password = [System.Web.Security.Membership]::GeneratePassword(12,3)
New-ADUser -UserPrincipalName $UPN -Name $Name -GivenName $FirstInitial -Surname $LastName -DisplayName $DisplayName -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true
Write-Host "Username:" $UPN "Password:" $Password
}