使用 PowerShell 建立 Microsoft 365 使用者帳戶Create Microsoft 365 user accounts with PowerShell

本文適用於 Microsoft 365 企業版和 Office 365 企業版。This article applies to both Microsoft 365 Enterprise and Office 365 Enterprise.

您可以使用 Microsoft 365 的 PowerShell,有效地建立使用者帳戶,包括多個帳戶。You can use PowerShell for Microsoft 365 to efficiently create user accounts, including multiple accounts.

當您在 PowerShell 中建立使用者帳戶時,永遠都必須有特定的帳戶屬性。When you create user accounts in PowerShell, certain account properties are always required. 不需要其他屬性,但很重要。Other properties aren't required but are important. 請參閱以下表格。See the following table.

屬性名稱Property name 必要?Required? 描述Description
DisplayNameDisplayName
Yes
這是 Microsoft 365 服務中使用的顯示名稱。This is the display name that's used in Microsoft 365 services. 例如, Caleb sills 帳戶For example, Caleb Sills.
UserPrincipalNameUserPrincipalName
Yes
這是用來登入 Microsoft 365 服務的帳戶名稱。This is the account name that's used to sign in to Microsoft 365 services. 例如, CalebS @ contoso.onmicrosoft.comFor example, CalebS@contoso.onmicrosoft.com.
FirstNameFirstName
No
LastNameLastName
No
LicenseAssignmentLicenseAssignment
No
這是授權方案 (也稱為授權計畫或 SKU) ,可將可用的授權指派給使用者帳戶。This is the licensing plan (also known as the license plan or SKU) from which an available license is assigned to the user account. 授權會定義帳戶可使用的 Microsoft 365 服務。The license defines the Microsoft 365 services that are available to the account. 當您建立帳戶時,您不需要指派授權給使用者,但是該帳戶必須具有存取 Microsoft 365 服務的授權。You don't have to assign a license to a user when you create the account, but the account must have a license to access Microsoft 365 services. 建立使用者帳戶之後,您有 30 天的時間進行使用者帳戶授權。You have 30 days to license the user account after you create it.
PasswordPassword
No
如果您未指定密碼,則會指派隨機密碼給使用者帳戶,並可在命令結果中看見此密碼。If you don't specify a password, a random password is assigned to the user account, and the password is visible in the results of the command. 如果您指定密碼,必須是下列類型的8到16個 ASCII 文字字元:小寫字母、大寫字母、數位和符號。If you specify a password, it needs to be 8 to 16 ASCII text characters of the following types: lowercase letters, uppercase letters, numbers, and symbols.
UsageLocationUsageLocation
No
這是有效的 ISO 3166-1 alpha-2 國碼。This is a valid ISO 3166-1 alpha-2 country code. 例如,美國 為美國,法國為 FRFor example, US for the United States, and FR for France. 提供此值很重要,因為某些國家/地區沒有提供某些 Microsoft 365 服務。It's important to provide this value, because some Microsoft 365 services aren't available in certain countries. 除非帳戶已設定此值,否則您無法將授權指派給使用者帳戶。You can't assign a license to a user account unless the account has this value configured. 如需詳細資訊,請參閱 關於授許可權制For more information, see About license restrictions.

注意

瞭解如何使用 Microsoft 365 系統管理中心建立使用者帳戶Learn how to create user accounts by using the Microsoft 365 admin center.

如需其他資源的清單,請參閱 管理使用者和群組For a list of additional resources, see Manage users and groups.

針對 Graph 模組,請使用 Azure Active Directory PowerShellUse the Azure Active Directory PowerShell for Graph module

首先,連線 至您的 Microsoft 365 租使用者First, connect to your Microsoft 365 tenant.

連接後,請使用下列語法來建立個別帳戶:After you connect, use the following syntax to create an individual account:

$PasswordProfile=New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password="<user account password>"
New-AzureADUser -DisplayName "<display name>" -GivenName "<first name>" -SurName "<last name>" -UserPrincipalName <sign-in name> -UsageLocation <ISO 3166-1 alpha-2 country code> -MailNickName <mailbox name> -PasswordProfile $PasswordProfile -AccountEnabled $true

本範例會為 US user Caleb sills 帳戶建立帳戶:This example creates an account for the US user Caleb Sills:

$PasswordProfile=New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password="3Rv0y1q39/chsy"
New-AzureADUser -DisplayName "Caleb Sills" -GivenName "Caleb" -SurName "Sills" -UserPrincipalName calebs@contoso.onmicrosoft.com -UsageLocation US -MailNickName calebs -PasswordProfile $PasswordProfile -AccountEnabled $true

使用適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組。Use the Microsoft Azure Active Directory Module for Windows PowerShell

首先,連線 至您的 Microsoft 365 租使用者First, connect to your Microsoft 365 tenant.

建立個別使用者帳戶Create an individual user account

若要建立個別帳戶,請使用下列語法:To create an individual account, use the following syntax:

New-MsolUser -DisplayName <display name> -FirstName <first name> -LastName <last name> -UserPrincipalName <sign-in name> -UsageLocation <ISO 3166-1 alpha-2 country code> -LicenseAssignment <licensing plan name> [-Password <Password>]

注意

PowerShell 核心不支援 Windows PowerShell 模組的 Microsoft Azure Active Directory 模組,以及其名稱中具有 Msol 的 Cmdlet。PowerShell Core doesn't support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets that have Msol in their name. 從 Windows PowerShell 執行這些 Cmdlet。Run these cmdlets from Windows PowerShell.

若要列出可用的授權方案名稱,請使用此命令:To list the available licensing plan names, use this command:

Get-MsolAccountSku

本範例會為 US user Caleb sills 帳戶建立帳戶,並指派 contoso:ENTERPRISEPACK (Office 365 Enterprise E3) 授權方案的授權。This example creates an account for the US user Caleb Sills, and assigns a license from the contoso:ENTERPRISEPACK (Office 365 Enterprise E3) licensing plan.

New-MsolUser -DisplayName "Caleb Sills" -FirstName Caleb -LastName Sills -UserPrincipalName calebs@contoso.onmicrosoft.com -UsageLocation US -LicenseAssignment contoso:ENTERPRISEPACK

建立多個使用者帳戶Create multiple user accounts

  1. 建立包含必要使用者帳戶資訊的逗點分隔值 (CSV) 檔案。例如:Create a comma-separated value (CSV) file that contains the required user account information. For example:

    UserPrincipalName,FirstName,LastName,DisplayName,UsageLocation,AccountSkuId
    ClaudeL@contoso.onmicrosoft.com,Claude,Loiselle,Claude Loiselle,US,contoso:ENTERPRISEPACK
    LynneB@contoso.onmicrosoft.com,Lynne,Baxter,Lynne Baxter,US,contoso:ENTERPRISEPACK
    ShawnM@contoso.onmicrosoft.com,Shawn,Melendez,Shawn Melendez,US,contoso:ENTERPRISEPACK
    

    注意

    CSV 檔案的第一列中的欄名稱和其順序都是任意的。The column names and their order in the first row of the CSV file are arbitrary. 不過,請確定檔案的其餘部分中的資料順序符合欄名稱的順序。But make sure the order of the data in the rest of the file matches the order of the column names. ,並在 PowerShell for Microsoft 365 命令中使用參數值的資料行名稱。And use the column names for the parameter values in the PowerShell for Microsoft 365 command.

  2. 使用下列語法:Use the following syntax:

     Import-Csv -Path <Input CSV File Path and Name> | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId [-Password $_.Password]} | Export-Csv -Path <Output CSV File Path and Name>
    

    本範例會從檔案 C:\My Documents\NewAccounts.csv 中建立使用者帳戶,並將結果記錄在名為 C:\My Documents\NewAccountResults.csv檔案中。This example creates user accounts from the file C:\My Documents\NewAccounts.csv and logs the results in a file named C:\My Documents\NewAccountResults.csv.

    Import-Csv -Path "C:\My Documents\NewAccounts.csv" | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path "C:\My Documents\NewAccountResults.csv"
    
  3. 檢閱輸出檔以查看結果。Review the output file to see the results. 我們未指定密碼,所以在輸出檔中會顯示 Microsoft 365 所產生的隨機密碼。We didn't specify passwords, so the random passwords that Microsoft 365 generated are visible in the output file.

請參閱See also

以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組Manage Microsoft 365 user accounts, licenses, and groups with PowerShell

使用 PowerShell 管理 Microsoft 365Manage Microsoft 365 with PowerShell

開始使用適用於 Microsoft 365 的 PowerShellGetting started with PowerShell for Microsoft 365