使用 CSV 批量创建 Active Directory 用户
如何使用 PowerShell 批量创建 Active Directory 用户
如果您想了解如何使用 CSV 文件批量创建 Active Directory 用户,
本文将帮助您逐步指导批量创建 Active Directory 用户。
Active Directory 对于实施它的每个组织都发挥着重要作用。
手动创建AD用户会消耗大量时间,可以通过运行有助于快速创建用户的PowerShell脚本来节省时间。
这个PowerShell脚本一次性创建多个用户,节省了大量时间。
如果您需要创建批量用户,您应该运行 PowerShell 脚本,而不是手动创建用户。
格式化 Excel 文件以另存为 CSV
要实现批量创建 Active Directory 用户的任务,首先必须格式化 Excel 文件,在其中放置所有必填字段。
您可以为用户的特定属性创建字段,我们在 AD 中的用户属性中看到这些字段。
然后用所需的名称、其他属性(例如部门和 OU)填充此 Excel 文件。
要设置 OU,您应该找到 OU 的专有名称
因此,您可以通过右键单击 OU 并单击“属性编辑器”,然后双击来找到 DN 名称区分名称。

一旦您在所有字段中填写了您的用户名及其属性以及 OU,
下图显示了字段和其他属性。


现在您可以尝试将文件保存为 CSV 格式
要将文件保存为 csv 格式,请单击“文件”,然后单击“另存为”,浏览位置,然后选择 csv 格式。


现在文件已保存为 csv 格式。
现在我们可以将此 CSV 文件放在我们的服务器上。
在我们的例子中,我们将其放置在位置 c:tempbulkusers.csv
因此,CSV 文件的名称为bulkusers.csv,保存在C 盘的temp 目录中。
运行 PowerShell 脚本在 AD 中创建批量用户
完成批量用户创建任务
我们必须运行以下脚本。
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Store the data from bulkusers.csv in the $ADUsers variable
$ADUsers = Import-csv C:tempbulkusers.csv
#Loop through each row containing user details in the CSV file
foreach ($User in $ADUsers)
{
#Read user data from each field in each row and assign the data to a variable as below
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$OU = $User.ou #This field refers to the OU the user account is to be created in
$email = $User.email
$streetaddress = $User.streetaddress
$city = $User.city
$zipcode = $User.zipcode
$state = $User.state
$country = $User.country
$telephone = $User.telephone
$jobtitle = $User.jobtitle
$company = $User.company
$department = $User.department
$Password = $User.Password
#Check to see if the user already exists in AD
if (Get-ADUser -F {SamAccountName -eq $Username})
{
#If user does exist, give a warning
Write-Warning "A user account with username $Username already exist in Active Directory."
}
else
{
#User does not exist then proceed to create the new user account
#Account will be created in the OU provided by the $OU variable read from the CSV file
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "[email protected]" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Firstname $Lastname" `
-Path $OU `
-City $city `
-Company $company `
-State $state `
-StreetAddress $streetaddress `
-OfficePhone $telephone `
-EmailAddress $email `
-Title $jobtitle `
-Department $department `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False
}
}
确保将您的域名放入用户主体名称中


完成格式化和编辑 UPN 后
现在您只需复制脚本并将其粘贴到 PowerShell 中即可
因此,运行该脚本后,您将得到以下结果,并且所有用户都将被创建。


现在脚本成功运行了
但是,如果我们去检查特定的 OU,例如我们案例中的 Marketing。
我们发现所需的用户现已创建。


如果检查组织下用户的属性。
最后,您将看到我们在 CSV 文件中设置的相关部门和公司。


结论
每当您需要在 Active Directory 中创建多个用户时。
最好使用PowerShell脚本批量创建用户以节省时间。
现在,您已经了解了如何在 Active Directory 中创建批量用户。
如果您有任何与本文相关的问题,请随时联系。
阅读更多:如何将 Google Chrome 保存的密码导出到 CSV 文件
您可能还喜欢 Active Directory 教程的其他一些相关帖子
此外,如果您想观看相同的视频,可以在下面查看。
