使用 Office 365 PowerShell 创建用户帐户Create user accounts with Office 365 PowerShell

摘要: 了解如何使用 Office 365 PowerShell 在 Office 365 中创建用户帐户。Summary: Learn how to use Office 365 PowerShell to create user accounts in Office 365.

您可以使用 Office 365 PowerShell 来高效地创建用户帐户,尤其是多个用户帐户。当您在 Office 365 PowerShell 中创建用户帐户时,某些帐户属性始终是必需的。其他属性对于创建帐户则不是必需的,但也很重要。下表介绍了这些属性:You can use Office 365 PowerShell to efficiently create user accounts, especially multiple user accounts. When you create user accounts in Office 365 PowerShell, certain account properties are always required. Other properties aren't required to create the account, but are otherwise important. These properties are described in the following table:

属性名称Property name 是否必需?Required? 说明Description
DisplayNameDisplayName
Yes
这是在 Office 365 服务中使用的显示名称。例如,Caleb Sills。This is the display name that's used in Office 365 services. For example, Caleb Sills.
UserPrincipalNameUserPrincipalName
Yes
这是用于登录到 Office 365 服务的帐户名称。例如,CalebS@contoso.onmicrosoft.com。This is the account name that's used to sign in to Office 365 services. For example, CalebS@contoso.onmicrosoft.com.
FirstNameFirstName
No
LastNameLastName
No
LicenseAssignmentLicenseAssignment
No
这是许可计划(也称为许可证计划、Office 365 计划或 SKU),使用它可以将可用的许可证分配给用户帐户。该许可证定义可供帐户使用的 Office 365 服务。当你创建帐户时,没有向用户分配许可证,但该帐户需要许可证才能访问 Office 365 服务。创建用户帐户后,有 30 天的时间可以对该用户帐户授权。This is the licensing plan (also known as the license plan, Office 365 plan, or SKU) from which an available license is assigned to the user account. The license defines the Office 365 services that are available to account. You don't have to assign a license to a user when you create the account, but the account requires a license to access Office 365 services. You have 30 days to license the user account after you create it.
密码Password
No
如果你没有指定密码,将向用户帐户分配一个随机密码,且该密码将显示在命令结果中。如果你指定了密码,则需要是以下三种类型的 8 到 16 位 ASCII 文本字符:小写字母、大写字母、数字和符号。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. If you specify a password, it needs to be 8 to 16 ASCII text characters from any three of the following types: lowercase letters, uppercase letters, numbers, and symbols.
UsageLocationUsageLocation
No
这是一个由两位字母组成的有效 ISO 3166-1 国家/地区代码。例如,US 代表美国,FR 代表法国。请务必提供此值,因为某些 Office 365 服务在某些国家不可用,因此不能为用户帐户分配许可证,除非该帐户已配置此值。有关详细信息,请参阅关于许可证限制This is a valid ISO 3166-1 alpha-2 country code. For example, US for the United States, and FR for France. It's important to provide this value, because some Office 365 services aren't available in certain countries, so you can't assign a license to a user account unless the account has this value configured. For more information, see About license restrictions.

使用用于图表模块的 Azure Active Directory PowerShellUse the Azure Active Directory PowerShell for Graph module

首先,连接到 Office 365 租户First, connect to your Office 365 tenant.

连接后,使用下列语法创建个人帐户:After you have connected, 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 United States user named 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

首先,连接到 Office 365 租户First, connect to your Office 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 不支持 Windows PowerShell 模块的 Microsoft Azure Active Directory 模块以及在其名称中带有Msol的 cmdlet。PowerShell Core does not support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets with Msol in their name. 若要继续使用这些 cmdlet,必须从 Windows PowerShell 运行它们。To continue using these cmdlets, you must run them 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 United States user named 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 文件的第一行中的顺序是任意的,但请确保文件其余部分中的数据与列名称的顺序相匹配,并使用列名称作为 Office 365 PowerShell 命令中的参数值。The column names and their order in the first row of the CSV file are arbitrary, but make sure the data in the rest of the file matches the order of the column names, and use the column names for the parameter values in the Office 365 PowerShell command.

  1. 使用以下语法: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 the user accounts from the file named C:\My Documents\NewAccounts.csv, and logs the results in the 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"
  1. 查看输出文件以查看结果。我们没有指定密码,这样便于在输出文件中显示 Office 365 生成的随机密码。Review the output file to see the results. We didn't specify passwords, so the random passwords that Office 365 generated are visible in the output file.

另请参阅See also

使用 Office 365 PowerShell 管理用户帐户和许可证Manage user accounts and licenses with Office 365 PowerShell

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

Office 365 PowerShell 入门Getting started with Office 365 PowerShell