如何使用PowerShell創建Active Directory用戶
如果您想學習如何使用CSV文件創建Active Directory用戶,請
本文將為您提供批量創建Active Directory用戶的分步指南。
Active Directory對實施其實施的每個組織都起著重要作用。
創建廣告用戶可以手動消耗大量時間,可以通過運行有助於快速創建用戶的PowerShell腳本來保存。
此PowerShell腳本一次創建多個用戶,節省了很多時間。
如果您需要創建批量用戶,則應運行PowerShell腳本,而不是手動創建用戶。
格式化Excel文件以保存為CSV
為了完成批量創建Active Directory用戶的任務,首先,您必須格式化Excel文件,並在其中包含所有必需的字段。
您可以為用戶創建特定屬性的字段,我們在用戶在AD中的屬性中看到的字段。
然後用所需的名稱,其他屬性,例如部門和OU填充此Excel文件。
要設置OU,您應該找到OU的傑出名稱
因此,您可以通過右鍵單擊OU並單擊屬性編輯器,然後雙擊來找到DN名稱區分名稱。
一旦您將所有字段填充了您的用戶名及其屬性,
下圖顯示了字段和其他屬性。
現在您可以嘗試以CSV格式保存文件
要以CSV格式保存文件,請單擊文件,然後單擊另存為,瀏覽位置,然後選擇格式為CSV。
現在,這些文件已保存在CSV格式中。
現在,我們可以將此CSV文件放在服務器上。
在我們的情況下,我們將其放在位置C:tempbulkusers.csv
因此,CSV文件的名稱是Bulkusers.csv保存在C驅動器內的臨時目錄中。
運行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 "" `
-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,例如在我們的情況下進行營銷。
我們發現現在創建了所需的用戶。
如果您檢查組織下的用戶的屬性。
最後,您將看到我們在CSV文件中設置的相關部門和公司。
結論
每當您需要在Active Directory中創建多個用戶時。
最好使用PowerShell腳本批量創建用戶來節省時間。
因此,現在您了解瞭如何在Active Directory中創建批量用戶。
如果您有與此帖子有關的任何問題,請隨時聯繫。
您可能還喜歡其他一些相關帖子到Active Directory教程
此外,如果您想查看視頻相同的視頻,則可以在下面查看。