使用 PowerShell 创建 Microsoft 365 用户帐户
此文章适用于 Microsoft 365 企业版和 Office 365 企业版。
可以使用适用于 Microsoft 365 的 PowerShell 有效地创建用户帐户,包括多个帐户。
在 PowerShell 中创建用户帐户时,始终需要某些帐户属性。 其他属性不是必需的,但很重要。 请参阅下表。
属性名称 | 是否必需? | 说明 |
---|---|---|
DisplayName |
是 |
这是 Microsoft 365 服务中使用的显示名称。 例如 ,Caleb Sills。 |
UserPrincipalName |
是 |
这是用于登录 Microsoft 365 服务的帐户名称。 例如,CalebS@contoso.onmicrosoft.com。 |
FirstName |
否 |
|
LastName |
否 |
|
LicenseAssignment |
否 |
这是许可计划 (也称为 许可证计划或 SKU) ,从中将可用许可证分配给用户帐户。 许可证定义帐户可用的 Microsoft 365 服务。 创建帐户时,无需向用户分配许可证,但该帐户必须具有访问 Microsoft 365 服务的许可证。 创建用户帐户后,您有 30 天的时间可以对该用户帐户授权。 |
密码 |
否 |
如果您没有指定密码,将向用户帐户分配一个随机密码,且该密码将显示在命令结果中。 如果指定密码,则密码必须是以下类型的 8 到 16 个 ASCII 文本字符:小写字母、大写字母、数字和符号。 |
UsageLocation |
否 |
这是一个由两位字母组成的有效 ISO 3166-1 国家/地区代码。 例如,US 用于美国,FR 用于法国。 提供此值非常重要,因为某些 Microsoft 365 服务在某些国家/地区不可用。 除非该帐户配置了此值,否则无法将许可证分配给用户帐户。 有关详细信息,请参阅关于许可证限制。 |
使用用于图表模块的 Azure Active Directory PowerShell
首先, 连接到 Microsoft 365 租户。
连接后,使用以下语法创建单个帐户:
$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 创建一个帐户:
$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
使用Microsoft Azure Active Directory模块进行Windows PowerShell
首先, 连接到 Microsoft 365 租户。
创建单个用户帐户
若要创建单个帐户,请使用下面的语法:
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>]
注意
对于名称中具有 Msol 的Windows PowerShell模块和 cmdlet,PowerShell Core 不支持 Microsoft Azure Active Directory 模块。 从 Windows PowerShell 运行这些 cmdlet。
若要列出可用的 许可计划名称,请使用以下命令:
Get-MsolAccountSku
此示例为美国用户 Caleb Sills 创建一个帐户,并从 (Office 365 企业版 E3) 许可计划中分配许可证contoso:ENTERPRISEPACK
。
New-MsolUser -DisplayName "Caleb Sills" -FirstName Caleb -LastName Sills -UserPrincipalName calebs@contoso.onmicrosoft.com -UsageLocation US -LicenseAssignment contoso:ENTERPRISEPACK
创建多个用户帐户
创建包含所需用户帐户信息的以逗号分隔值 (CSV) 文件。 例如:
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 文件第一行中的列名及其顺序是任意的。 但请确保文件其余部分中的数据顺序与列名称的顺序匹配。 并在 PowerShell for Microsoft 365 命令中使用参数值的列名称。
使用以下语法:
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的文件中。
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"
查看输出文件以查看结果。 我们没有指定密码,因此 Microsoft 365 生成的随机密码在输出文件中可见。
另请参阅
使用 PowerShell 管理 Microsoft 365 用户帐户、许可证和组