使用 PowerShell 从 CSV 导入 AD 用户
有时您希望将用户批量导入到 Active Directory 中。您已经将 AD 用户导出到 CSV 文件。但是,现在您想使用该 CSV 文件将用户导入回 AD。最快、最优秀的方法是使用 PowerShell。在本文中,您将了解如何使用 PowerShell 从 CSV 导入 AD 用户。
Import-ADUsers.ps1 PowerShell 脚本将遍历 CSV 文件并创建 AD 用户,每个用户包含以下信息:
- 姓名
- 名
- 姓
- 显示名称
- 用户登录名
- 用户主体名称
- 街道
- 城市
- 状态
- 邮政编码
- 国家
- 职称
- 部门
- 公司
- 经理
- 或者
- 描述
- 办公室
- 电话号码
- 电子邮件
- 移动的
- 笔记
- 账户状态
笔记:该帐户的密码将设置为P@ssw0rd1234。在 PowerShell 脚本中将其更改为不同的密码。
如何将用户从 CSV 文件导入到 Active Directory
让我们完成这些步骤并使用 PowerShell 从 CSV 文件批量导入 Active Directory 用户。
第 1 步:与用户一起创建 CSV 文件
如果没有 CSV 文件,您将无法使用该脚本并将用户导入到 AD 中。
了解更多:使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户
- 如果您已有 Active Directory,则可以运行使用 PowerShell 将 AD 用户导出到 CSV 一文中的脚本。之后,使用该 CSV 文件。
- 假设您没有 Active Directory 将 AD 用户导出到 CSV 文件,而只需要 CSV 文件示例,以便您可以编辑和使用它;下载 CSV 文件 ImportADUsers.csv。
下载 Import-ADUsers.ps1 PowerShell 脚本并将其放置在域控制器上C:脚本文件夹。如果您没有脚本文件夹,请创建一个。
确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。
另一种选择是将以下代码复制并粘贴到记事本中。给它起个名字导入-ADUsers.ps1并将其放置在C:脚本文件夹。
<#
.SYNOPSIS
Import-ADUsers.ps1
.DESCRIPTION
Import Active Directory users from CSV file.
.LINK
alitajran.com/import-ad-users-from-csv-powershell
.NOTES
Written by: ALI TAJRAN
Website: alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V2.00, 02/11/2024 - Refactored script
#>
# Define the CSV file location and import the data
$Csvfile = "C:tempImportADUsers.csv"
$Users = Import-Csv $Csvfile
# The password for the new user
$Password = "P@ssw0rd1234"
# Import the Active Directory module
Import-Module ActiveDirectory
# Loop through each user
foreach ($User in $Users) {
try {
# Retrieve the Manager distinguished name
$managerDN = if ($User.'Manager') {
Get-ADUser -Filter "DisplayName -eq '$($User.'Manager')'" -Properties DisplayName |
Select-Object -ExpandProperty DistinguishedName
}
# Define the parameters using a hashtable
$NewUserParams = @{
Name = "$($User.'First name') $($User.'Last name')"
GivenName = $User.'First name'
Surname = $User.'Last name'
DisplayName = $User.'Display name'
SamAccountName = $User.'User logon name'
UserPrincipalName = $User.'User principal name'
StreetAddress = $User.'Street'
City = $User.'City'
State = $User.'State/province'
PostalCode = $User.'Zip/Postal Code'
Country = $User.'Country/region'
Title = $User.'Job Title'
Department = $User.'Department'
Company = $User.'Company'
Manager = $managerDN
Path = $User.'OU'
Description = $User.'Description'
Office = $User.'Office'
OfficePhone = $User.'Telephone number'
EmailAddress = $User.'E-mail'
MobilePhone = $User.'Mobile'
AccountPassword = (ConvertTo-SecureString "$Password" -AsPlainText -Force)
Enabled = if ($User.'Account status' -eq "Enabled") { $true } else { $false }
ChangePasswordAtLogon = $true # Set the "User must change password at next logon"
}
# Add the info attribute to OtherAttributes only if Notes field contains a value
if (![string]::IsNullOrEmpty($User.Notes)) {
$NewUserParams.OtherAttributes = @{info = $User.Notes }
}
# Check to see if the user already exists in AD
if (Get-ADUser -Filter "SamAccountName -eq '$($User.'User logon name')'") {
# Give a warning if user exists
Write-Host "A user with username $($User.'User logon name') already exists in Active Directory." -ForegroundColor Yellow
}
else {
# User does not exist then proceed to create the new user account
# Account will be created in the OU provided by the $User.OU variable read from the CSV file
New-ADUser @NewUserParams
Write-Host "The user $($User.'User logon name') is created successfully." -ForegroundColor Green
}
}
catch {
# Handle any errors that occur during account creation
Write-Host "Failed to create user $($User.'User logon name') - $($_.Exception.Message)" -ForegroundColor Red
}
}
- 21号线:编辑 CSV 文件位置。
- 25号线:更改密码。
以管理员身份运行 PowerShell 并运行 PowerShell 脚本以从 CSV 文件导入 AD 用户。等到它完成。
C:scripts.Import-ADUsers.ps1
该脚本将显示是否:
- 用户创建成功。
- 该用户已经存在。
- 无法创建用户并显示错误消息。
A user with username Kylie.Davidson already exists in Active Directory.
A user with username Leonard.Clark already exists in Active Directory.
A user with username Madeleine.Fisher already exists in Active Directory.
A user with username Melanie.Scott already exists in Active Directory.
A user with username Nicholas.Murray already exists in Active Directory.
A user with username Piers.Bower already exists in Active Directory.
A user with username Ruth.Dickens already exists in Active Directory.
The user Sebastian.Nolan is created successfully.
The user Zoe.Roberts is created successfully.
验证用户是否已在 Active Directory 中成功创建。
结论
您了解了如何使用 PowerShell 从 CSV 导入 AD 用户。如果您使用导出 AD 用户 PowerShell 脚本,并且想要将用户导入回 Active Directory,请运行导入 AD 用户 PowerShell 脚本。
您喜欢这篇文章吗?您可能还喜欢从 Active Directory 导出禁用的用户。不要忘记关注我们并分享这篇文章。
