使用 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 SillsFor 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.
密码Password
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 国家/地区代码。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.

使用用于图表模块的 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

此示例为美国用户 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 Core 不支持在其名称中包含 Msol 的 Windows PowerShell 模块和 Cmdlet 的 Microsoft Azure Active Directory 模块。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

本示例为美国用户 Caleb Sills创建一个帐户,并从 contoso:ENTERPRISEPACK (Office 365 企业版 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>
    

    此示例从 file 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

PowerShell for Microsoft 365 入门Getting started with PowerShell for Microsoft 365