共用方式為


在 Microsoft Entra ID 中大量建立使用者

Microsoft Entra 識別碼,Microsoft Entra 的一部分,支援大量使用者建立和刪除作業,並支援下載使用者清單。 只要填寫逗號分隔值 (CSV) 範本,您就可以從 Microsoft Entra ID 下載。

所需的權限

若要在系統管理入口網站中大量建立使用者,您必須至少以用戶系統管理員身分登入。

了解 CSV 範本

下載並填入大量上傳 CSV 範本,以協助您成功建立大量Microsoft Entra 使用者。 您下載的 CSV 範本可能看起來會是此範例:

用於上傳和圖說的電子錶格螢幕快照,其中說明每個數據列和數據行的用途和值。

警告

如果您使用 CSV 範本只新增一個專案,則必須保留第 3 列,並將新的專案新增至第 4 列。

請確定您新增擴展名,.csv並在、 passwordProfileaccountEnabled之前userPrincipalName移除任何前置空格。

CSV 範本結構

下載的 CSV 範本資料列為以下:

  • 版本號碼:上傳 CSV 的第一列必須包含版本號碼。
  • 資料行標題:資料行標題的格式為<項目名稱> [屬性名稱]<必要或空白>。 例如: Name [displayName] Required 。 部分較舊版本的範本可能有些微變化。
  • 範例資料列:我們已在範本的各資料行中加入一列可接受的範例值。 您必須移除範本列並取代為您本身的項目。

其他指導方針

  • 上傳範本的前面兩列不得移除或修改,否則無法進行上傳。
  • 首先會列出必要的資料行。
  • 我們不建議在範本中新增新資料行。 您新增的任何其他資料行已略過或尚未處理。
  • 我們建議您盡可能下載最新版本的 CSV 範本。
  • 請務必在任何欄位之前/之後檢查沒有任何非預期的空格符。 針對 用戶主體名稱,具有這類空格符會導致匯入失敗。
  • 請確定初始密碼中的值符合目前使用中的密碼原則

大量建立使用者

  1. 以至少是使用者管理員的身分登入 Microsoft Entra 系統管理中心

  2. 選取 [Microsoft Entra ID]。

  3. 選取 [所有使用者>>大量建立]。

  4. 在 [ 大量建立使用者 ] 頁面上,選取 [下載 ] 以接收用戶屬性的有效逗號分隔值 (CSV) 檔案,然後新增您想要建立的使用者。

    顯示如何選取您要在其中列出要新增之使用者的本機 CSV 檔案的螢幕快照。

  5. 開啟 CSV 檔案,併為您想要建立的每個使用者新增一行。 唯一的必要值為 Name用戶主體名稱初始密碼[封鎖登入] (是/否)。 然後儲存檔案。

    顯示 CSV 檔案範例的螢幕快照,其中包含要建立之使用者的名稱和標識碼。

  6. 在 [ 大量建立使用者 ] 頁面上的 [上傳 CSV 檔案] 底覽至檔案。 當您選取檔案並按兩下 [ 提交] 時,CSV 檔案的驗證就會開始。

  7. 驗證檔案內容之後,您會看到 已成功上傳檔案。 如果發生錯誤,您必須修正這些錯誤,才能提交作業。

  8. 當您的檔案通過驗證時,選取 [ 提交 ] 以啟動匯入新使用者的大量作業。

  9. 匯入作業完成時,您會看到大量作業作業狀態的通知。

如果您遇到錯誤,您可以在 [大量作業結果] 頁面上下載並檢視結果檔案。 此檔案包含每個錯誤的原因。 檔案提交必須符合提供的範本,並包含確切的數據行名稱。 如需大量作業限制的詳細資訊,請參閱 大容量導入服務限制

檢查狀態

您可以在 [大量作業結果] 頁面中看到所有暫止大量要求的狀態。

顯示如何在大量作業結果頁面中檢查作業狀態的螢幕快照。

接下來,您可以檢查您建立的使用者是否存在於 Microsoft Entra 組織中,Azure 入口網站 或是使用 PowerShell。

確認使用者

  1. 以至少是使用者管理員的身分登入 Microsoft Entra 系統管理中心
  2. 選取 [Microsoft Entra ID]。
  3. 選取 [所有使用者>]。
  4. 在 [顯示] 底下,選取 [所有使用者],並確認您建立的使用者已列出。

使用 PowerShell 驗證使用者

執行以下命令:

Get-MgUser -Filter "UserType eq 'Member'"

您應該會看到您所建立的使用者已列出。

大容量導入服務限制

您應該知道,每個大量作業活動最多可以執行一小時。

Microsoft Entra 管理入口網站中的大量作業可能會在非常大型的租使用者上逾時並失敗。 這項限制是已知問題,因為調整限制。 Microsoft工程小組正致力於最終解決這項限制的新服務。

注意

執行大量作業,例如匯入或建立時,如果大量作業未在一小時內完成,可能會遇到問題。 若要解決此問題,建議您分割每個批次處理的記錄數目。 例如,在啟動匯出之前,您可以篩選群組類型或使用者名稱來限制結果集,以減少結果的大小。 藉由精簡篩選,基本上您會限制大量作業傳回的數據。

此問題的另一個因應措施是使用PowerShell直接Microsoft Graph API呼叫。 針對大量下載使用者和群組失敗,我們建議使用 PowerShell Cmdlet GET-MgGroup -AllGET-MgUser -All

下列 PowerShell 程式代碼範例適用於與下列相關的大量作業:

使用者

大量下載所有使用者

# Import the Microsoft Graph module 
Import-Module Microsoft.Graph 

# Authenticate to Microsoft Graph (you may need to provide your credentials) 
Connect-MgGraph -Scopes "User.Read.All" 

# Get all users using Get-MgUser 
$users = Get-MgUser -All -ConsistencyLevel eventual -Property Id, DisplayName, UserPrincipalName,UserType,OnPremisesSyncEnabled,CompanyName,CreationType 

# Specify the output CSV file path 
$outputCsvPath = "C:\\Users\\YourUsername\\Documents\\Users.csv"  

# Create a custom object to store user data 
$userData = @() 

# Loop through each user and collect relevant data 
foreach ($user in $users) { 
    $userObject = [PSCustomObject]@{ 
        Id = $user.Id 
        DisplayName = $user.DisplayName 
        UserPrincipalName = $user.UserPrincipalName 
        UserType = $user.UserType 
        OnPremisesSyncEnabled = $user.OnPremisesSyncEnabled 
        CompanyName = $user.CompanyName 
        CreationType = $user.CreationType 
    } 
    $userData += $userObject 
} 

# Export user data to a CSV file 
$userData | Export-Csv -Path $outputCsvPath -NoTypeInformation 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

Write-Host "User data exported to $outputCsvPath" 

大量建立使用者

# Import the Microsoft Graph module 
Import-Module Microsoft.Graph 

# Authenticate to Microsoft Graph (you may need to provide your credentials) 
Connect-MgGraph -Scopes "User.ReadWrite.All" 

# Specify the path to the CSV file containing user data 
$csvFilePath = "C:\\Path\\To\\Your\\Users.csv" 

# Read the CSV file (adjust the column names as needed) 
$usersData = Import-Csv -Path $csvFilePath 

# Loop through each row in the CSV and create users \
foreach ($userRow in $usersData) { 
    $userParams = @{ 
        DisplayName = $userRow.'Name [displayName] Required' 
        UserPrincipalName = $userRow.'User name [userPrincipalName] Required' 
        PasswordProfile = @{ 
            Password = $userRow.'Initial password [passwordProfile] Required' 
        } 
        AccountEnabled = $true 
        MailNickName = $userRow.mailNickName 
    } 
    try { 
        New-MgUser @userParams 
        Write-Host "User $($userRow.UserPrincipalName) created successfully." 
    } catch { 
        Write-Host "Error creating user $($userRow.UserPrincipalName): $($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

Write-Host "Bulk user creation completed." 

注意

請確定您的 CSV 檔案包含必要的資料列(例如、DisplayNameUserPrincipalName、 等等)。 此外,請調整文本,以符合 CSV 檔案中的實際數據行名稱。

大量刪除使用者

# Import the Microsoft Graph module 
Import-Module Microsoft.Graph 

# Authenticate to Microsoft Graph (you may need to provide your credentials) 
Connect-MgGraph -Scopes "User.ReadWrite.All" 

# Specify the path to the CSV file containing user data 
$csvFilePath = "C:\\Path\\To\\Your\\Users.csv" 

# Read the CSV file (adjust the column names as needed) 
$usersData = Import-Csv -Path $csvFilePath 

# Loop through each row in the CSV and delete users 
foreach ($userRow in $usersData) { 
    try { 
        Remove-MgUser -UserId $userRow.UserPrincipalName -Confirm:$false 
        Write-Host "User $($userRow.UserPrincipalName) deleted successfully." 
    } catch { 
        Write-Host "Error deleting user $($userRow.UserPrincipalName): $($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

Write-Host "Bulk user deletion completed." 

注意

請確定您的 CSV 檔案包含必要的資料列(例如 )。 UserPrincipalName 此外,請調整文本,以符合 CSV 檔案中的實際數據行名稱。

群組

大量下載所有群組

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "Group.Read.All" 

 # Get the group members 
 $groups = Get-MgGroup -All | Select displayName, Id, groupTypes,mail 

 # Create a custom object to store group data 
$groupData = @() 

# Loop through each group and collect relevant data 
foreach ($group in $groups) { 
    if ($group.groupTypes -contains "Unified"){$groupType = "Microsoft 365"} 
    else {$groupType = "Security"} 
    if ($group.groupTypes -contains "DynamicMembership"){$membershipType = "Dynamic"} 
    else {$membershipType = "Assigned"} 
    $groupObject = [PSCustomObject]@{ 
        Id = $group.Id 
        DisplayName = $group.displayName 
        Mail = $group.mail 
        GroupType = $groupType 
        MemebershipType = $membershipType 
    }   
    $groupData += $groupObject 
} 

 # Specify the output CSV file path 
 $outputCsvPath = "C:\\Users\\cewu\\Documents\\Groups.csv" 

 $groupData| Export-Csv -Path $outputCsvPath -NoTypeInformation 
 
 Write-Host "Group members exported to $outputCsvPath" 

大量下載群組成員

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "Group.Read.All,GroupMember.Read.All" 

 # Set the group ID of the group whose members you want to download 
 $groupId = "your_group_id" 

 # Get the group members 
 $members = Get-MgGroupMember -GroupId $groupId -All | select * -ExpandProperty additionalProperties | Select-Object @( 
                'id'     
                @{  Name       = 'userPrincipalName' 
                    Expression = { $_.AdditionalProperties["userPrincipalName"] } 
                } 
                @{  Name = 'displayName' 
                Expression = { $_.AdditionalProperties["displayName"] } 
                } 
            ) 

 # Specify the output CSV file path 
 $outputCsvPath = "C:\\Users\\YourUserName\\Documents\\GroupMembers.csv" 

 $members| Export-Csv -Path $outputCsvPath -NoTypeInformation 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

 Write-Host "Group members exported to $outputCsvPath"  

大量新增成員

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "GroupMember.ReadWrite.All" 

# Import the CSV file 
$members = Import-Csv -Path "C:\path\to\your\file.csv" 

# Define the Group ID 
$groupId = "your-group-id" 

# Iterate over each member and add them to the group 
foreach ($member in $members) { 
    try{ 
        New-MgGroupMember -GroupId $groupId -DirectoryObjectId $member.memberObjectId 
  	 Write-Host "Added $($member.memberObjectId) to the group."  
    } 
    Catch{ 
        Write-Host "Error adding member $($member.memberObjectId):$($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

大量移除成員

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "GroupMember.ReadWrite.All" 

# Import the CSV file 
$members = Import-Csv -Path "C:\path\to\your\file.csv" 

# Define the Group ID 
$groupId = "your-group-id" 

# Iterate over each member and add them to the group 
foreach ($member in $members) { 
    try{ 
        Remove-MgGroupMemberByRef -GroupId $groupId -DirectoryObjectId $member.memberObjectId \
        Write-Host "Removed $($member.memberObjectId) from the group." 
    } 
    Catch{ 
        Write-Host "Error removing member $($member.memberObjectId):$($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

裝置

大量下載所有裝置

Import-Module Microsoft.Graph 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "Device.Read.All" 

 # Get all devices  
 $devices = Get-MgDevice -All |select displayName,deviceId,operatingSystem,operatingSystemVersion,isManaged,isCompliant,mdmAppId,registeredOwners,TrustType 

 # Specify the output CSV file path 
 $outputCsvPath = "C:\\Users\\YourUserName\\Documents\\Devices.csv" 

 $devices| Export-Csv -Path $outputCsvPath -NoTypeInformation 

 Write-Host "Devices exported to $outputCsvPath"  

下一步