使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户
我们希望在公司中创建新的 Microsoft Entra ID 用户。通过 Microsoft Entra 管理中心并通过向导创建用户将花费我们一些时间。为了加快速度,我们将选择 PowerShell 来创建批量 Microsoft Entra ID 用户。在本文中,您将了解如何从 CSV 文件在 Microsoft Entra ID 中创建用户。
连接到 Microsoft Graph
在开始之前,您需要安装并连接到 Microsoft Graph。
以管理员身份启动 Windows PowerShell 并运行以下命令。
Install-Module Microsoft.Graph -Force
重要的:在运行 cmdlet 或脚本之前,请务必更新到最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。
运行连接-MgGraph用于启动与 Microsoft Entra ID 的连接的 cmdlet。
Connect-MgGraph -Scopes "User.ReadWrite.All"
要使用 PowerShell 创建 Microsoft Entra ID 用户,您需要最少的必需属性:
- -显示名称
- -邮件昵称
- -用户主体名称
- -密码配置文件
- - 帐户启用
使用 PowerShell 创建 Microsoft Entra ID 用户
启动 PowerShell ISE 或 Visual Studio Code 并运行新用户管理用于创建新的 Microsoft Entra ID 用户的 cmdlet。
# Create password profile
$PasswordProfile = @{
Password = "P@ssw0rd!"
ForceChangePasswordNextSignIn = $true
ForceChangePasswordNextSignInWithMfa = $true
}
# Create Microsoft Entra ID user
$UserParams = @{
DisplayName = "Jeff Baker"
MailNickName = "Jeff.Baker"
UserPrincipalName = "[email protected]"
PasswordProfile = $PasswordProfile
AccountEnabled = $true
}
New-MgUser @UserParams
运行该命令后,将显示用户创建成功的输出。
DisplayName Id Mail UserPrincipalName
----------- -- ---- -----------------
Jeff Baker 4cc4f605-8ed1-47f5-8c5f-ca9a29e8ec82 [email protected]
现在你已经知道如何使用 Microsoft Graph PowerShell 创建 Microsoft Entra ID 用户,接下来让我们看看如何使用 CSV 文件批量创建 Microsoft Entra ID 用户。
使用 PowerShell 创建批量 Microsoft Entra ID 用户
让我们完成从 CSV 文件创建批量 Microsoft Entra ID 用户的步骤。
1.下载CSV模板
下载 NewUsers.csv 并将 CSV 模板放置在路径中C:温度。如果你没有温度文件夹,创建一个。
调整 CSV 文件并在完成后保存。
2.下载Add-NewEntraIDUsers PowerShell脚本
下载 Add-NewEntraIDUsers.ps1 脚本并将其保存在路径中C:脚本。如果你没有脚本文件夹,创建一个。
推荐阅读:使用 CSV 批量创建 Active Directory 用户
确保文件未被阻止,以防止运行脚本时出现错误。请阅读文章运行 PowerShell 脚本时出现未数字签名错误来了解更多信息。
另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Add-NewEntraIDUsers.ps1 并将其放置在C:脚本文件夹。
<#
.SYNOPSIS
Add-NewEntraIDUsers.ps1
.DESCRIPTION
Create Microsoft Entra ID users from CSV file.
.LINK
www.alitajran.com/create-microsoft-entra-id-users
.NOTES
Written by: ALI TAJRAN
Website: www.alitajran.com
LinkedIn: linkedin.com/in/alitajran
.CHANGELOG
V1.00, 05/19/2023 - Initial version
#>
# Connect to Microsoft Graph with user read/write permissions
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Specify the path of the CSV file
$CSVFilePath = "C:tempNewUsers.csv"
# Create password profile
$PasswordProfile = @{
Password = "P@ssw0rd!"
ForceChangePasswordNextSignIn = $true
ForceChangePasswordNextSignInWithMfa = $true
}
# Import data from CSV file
$Users = Import-Csv -Path $CSVFilePath
# Loop through each row containing user details in the CSV file
foreach ($User in $Users) {
$UserParams = @{
DisplayName = $User.DisplayName
MailNickName = $User.MailNickName
UserPrincipalName = $User.UserPrincipalName
Department = $User.Department
JobTitle = $User.JobTitle
Mobile = $User.Mobile
Country = $User.Country
EmployeeId = $User.EmployeeId
PasswordProfile = $PasswordProfile
AccountEnabled = $true
}
try {
$null = New-MgUser @UserParams -ErrorAction Stop
Write-Host ("Successfully created the account for {0}" -f $User.DisplayName) -ForegroundColor Green
}
catch {
Write-Host ("Failed to create the account for {0}. Error: {1}" -f $User.DisplayName, $_.Exception.Message) -ForegroundColor Red
}
}
3. 运行 Add-NewEntraIDUsers PowerShell 脚本
将目录路径更改为C:脚本并运行脚本 Add-NewEntraIDUsers.ps1。
C:scripts.Add-NewEntraIDUsers.ps1
输出将通过消息显示用户是否成功创建。
Welcome To Microsoft Graph!
Successfully created the account for Amanda Morgan
Successfully created the account for Jeffrey Welch
Successfully created the account for Kevin Howard
Failed to create the account for Lauren Russell. Error: Another object with the same value for property userPrincipalName already exists.
4. 验证 Microsoft Entra ID 用户
运行下面的脚本,使用 Get-MgUser cmdlet 检索 Microsoft Entra ID 中新创建的用户。
# Specify the path of the CSV file
$CSVFilePath = "C:tempNewUsers.csv"
# Import the CSV file
$users = Import-Csv $CSVFilePath
foreach ($user in $users) {
if (Get-MgUser -UserId $user.UserPrincipalName -ErrorAction SilentlyContinue) {
Write-Host "$($user.UserPrincipalName) exists" -ForegroundColor Green
}
else {
Write-Host "$($user.UserPrincipalName) does not exist" -ForegroundColor Red
}
}
另一种方法是登录微软 Entra 管理中心并验证新创建的用户。
当用户登录时微软365,他们被迫更新密码。
这是否有助于您使用 PowerShell 从 CSV 文件创建 Microsoft Entra ID 用户?
笔记:假设您不想使用 PowerShell 并且希望通过 Microsoft Entra 管理中心创建用户;阅读文章使用 CSV 文件批量创建 Microsoft 365 用户。
结论
您了解了如何使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户。首先,准备 CSV 文件并确保填写所有信息。接下来,运行 Add-NewEntraIDUsers.ps1 脚本在 Microsoft Entra ID 中创建用户。最后一部分是检查用户是否创建成功。
您喜欢这篇文章吗?您可能还喜欢防止组织中的 MFA 疲劳攻击。不要忘记关注我们并分享这篇文章。
