使用 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 導出禁用的用戶。不要忘記關注我們並分享這篇文章。
