使用 PowerShell 创建 SharePoint Online 网站并添加用户Create SharePoint Online sites and add users with PowerShell

本文适用于 Microsoft 365 企业版和 Office 365 企业版。This article applies to both Microsoft 365 Enterprise and Office 365 Enterprise.

使用适用于 Microsoft 365 的 PowerShell 创建 SharePoint Online 网站和添加用户时,快速重复执行任务的速度比在 Microsoft 365 管理中心快得多。When you use PowerShell for Microsoft 365 to create SharePoint Online sites and add users, you can quickly and repeatedly perform tasks much faster than you can in the Microsoft 365 admin center. 还可以执行在 Microsoft 365 管理中心中无法执行的任务。You can also perform tasks that are not possible to perform in the Microsoft 365 admin center.

连接到 SharePoint OnlineConnect to SharePoint Online

本主题中的过程需要您连接到 SharePoint Online。The procedures in this topic require you to connect to SharePoint Online. 有关说明,请参阅 连接到 SharePoint Online PowerShellFor instructions, see Connect to SharePoint Online PowerShell

步骤 1:使用 PowerShell 创建新的网站集Step 1: Create new site collections using PowerShell

使用 PowerShell 创建多个网站,以及使用提供的示例代码和记事本创建的 .csv 文件。Create multiple sites using PowerShell and a .csv file that you create using the example code provided and Notepad. 在此步骤中,你将使用你自己的网站和租户特定信息替换括号中显示的占位符信息。For this procedure, you’ll be replacing the placeholder information shown in brackets with your own site- and tenant-specific information. 此过程允许你创建单个文件并运行使用该文件的单个 PowerShell 命令。This process lets you create a single file and run a single PowerShell command that uses that file. 这使得所采取的操作具有可重复性和可移植性,并可以减少许多(如果不是全部)因向 SharePoint Online 命令行管理程序键入长命令而导致的错误。This makes the actions taken both repeatable and portable and eliminates many, if not all, errors that can come from typing long commands into the SharePoint Online Management Shell. 此步骤包括两个部分。There are two parts to this procedure. 首先,您将创建一个 .csv 文件,然后使用 PowerShell 引用该 .csv 文件,它将使用该文件的内容来创建网站。First you’ll create a .csv file, and then you’ll reference that .csv file using PowerShell, which will use its contents to create the sites.

PowerShell cmdlet 导入 .csv 文件,然后通过管道将该文件通过管道传输至大括号内的循环,该循环将读取文件的第一行作为列标题。The PowerShell cmdlet imports the .csv file and pipes it to a loop inside the curly brackets that reads the first line of the file as column headers. 然后,PowerShell cmdlet 会访问其余记录,为每个记录创建一个新的网站集,然后根据列标题分配网站集的属性。The PowerShell cmdlet then iterates through the remaining records, creates a new site collection for each record, and assigns properties of the site collection according to the column headers.

创建 .csv 文件Create a .csv file

备注

资源配额参数仅适用于经典网站。The resource quota parameter works only on classic sites. 如果在新式网站上使用此参数,您可能会收到一条警告消息,指出已弃用此参数。If you use this parameter on a modern site, you may receive a warning message that it has been deprecated.

  1. 打开记事本,然后向其中粘贴以下文本块:Open Notepad, and paste the following text block into it:
Owner,StorageQuota,Url,ResourceQuota,Template,TimeZoneID,Name
owner@tenant.onmicrosoft.com,100,https://tenant.sharepoint.com/sites/TeamSite01,25,EHS#1,10,Contoso Team Site
owner@tenant.onmicrosoft.com,100,https://tenant.sharepoint.com/sites/Blog01,25,BLOG#0,10,Contoso Blog
owner@tenant.onmicrosoft.com,150,https://tenant.sharepoint.com/sites/Project01,25,PROJECTSITE#0,10,Project Alpha
owner@tenant.onmicrosoft.com,150,https://tenant.sharepoint.com/sites/Community01,25,COMMUNITY#0,10,Community Site


其中 ,tenant 是租户的名称 ,owner 是租户上要授予其主要网站集管理员角色的用户的用户名。Where tenant is the name of your tenant, and owner is the user name of the user on your tenant to whom you want to grant the role of primary site collection administrator.
(使用记事本更快地进行批量替换时,可以按 Ctrl+H。) (You can press Ctrl+H when you use Notepad to bulk replace faster.)

  1. 将桌面上的文件另存为 SiteCollections.csv。Save the file on your desktop as SiteCollections.csv.

提示

在使用此脚本文件或其他任何 .csv 或 Windows PowerShell 脚本文件之前,最佳做法是确保没有多余的或非打印字符。Before you use this or any other .csv or Windows PowerShell script file, it's a good practice to make sure that there are no extraneous or nonprinting characters. 在 Word 中打开该文件,在功能区单击“段落”图标以显示非打印字符。Open the file in Word, and in the ribbon, click the paragraph icon to show nonprinting characters. 应该没有多余的非打印字符。There should be no extraneous nonprinting characters. 例如,除了文件末尾的最后一个段落标记之外,应该没有其他任何段落标记。For example, there should be no paragraph marks beyond the final one at the end of the file.

运行 Windows PowerShell 命令Run the Windows PowerShell command

  1. 在Windows PowerShell,键入或复制并粘贴以下命令,然后按 Enter:At the Windows PowerShell prompt, type or copy and paste the following command, and press Enter:
Import-Csv C:\users\MyAlias\desktop\SiteCollections.csv | ForEach-Object {New-SPOSite -Owner $_.Owner -StorageQuota $_.StorageQuota -Url $_.Url -NoWait -ResourceQuota $_.ResourceQuota -Template $_.Template -TimeZoneID $_.TimeZoneID -Title $_.Name}


其中 MyAlias 等于您的用户别名。Where MyAlias equals your user alias.

  1. 等待 Windows PowerShell 提示符重新出现。可能需要一两分钟。Wait for the Windows PowerShell prompt to reappear. It might take a minute or two.

  2. 在 Windows PowerShell 提示符处键入或复制并粘贴以下 cmdlet,然后按 Enter 键:At the Windows PowerShell prompt, type or copy and paste the following cmdlet, and press Enter:

Get-SPOSite -Detailed | Format-Table -AutoSize

  1. 请注意列表中的新网站集。Note the new site collections in the list. 使用示例 CSV 文件,你将看到以下网站集 :TeamSite01、Blog01、Project01Community01 Using our example CSV file, you would see the following site collections: TeamSite01, Blog01, Project01, and Community01

就是这样。That’s it. 已使用创建的 .csv 文件和一个 Windows PowerShell 命令创建多个网站集。You’ve created multiple site collections using the .csv file you created and a single Windows PowerShell command. 现在,可以创建用户并将其分配给这些网站。You’re now ready to create and assign users to these sites.

步骤 2:添加用户和组Step 2: Add users and groups

现在,您将创建用户并将其添加到网站集组中。然后,您将使用 .csv 文件批量上载新的组和用户。Now you’re going to create users and add them to a site collection group. You will then use a .csv file to bulk upload new groups and users.

以下过程继续使用示例网站 TeamSite01、Blog01、Project01 和 Community01。The following procedures continue using the example sites TeamSite01, Blog01, Project01, and Community01.

创建 .csv 和 .ps1 文件Create .csv and .ps1 files

  1. 打开记事本,然后向其中粘贴以下文本块:Open Notepad, and paste the following text block into it:
Site,Group,PermissionLevels
https://tenant.sharepoint.com/sites/Community01,Contoso Project Leads,Full Control
https://tenant.sharepoint.com/sites/Community01,Contoso Auditors,View Only
https://tenant.sharepoint.com/sites/Community01,Contoso Designers,Design
https://tenant.sharepoint.com/sites/TeamSite01,XT1000 Team Leads,Full Control
https://tenant.sharepoint.com/sites/TeamSite01,XT1000 Advisors,Edit
https://tenant.sharepoint.com/sites/Blog01,Contoso Blog Designers,Design
https://tenant.sharepoint.com/sites/Blog01,Contoso Blog Editors,Edit
https://tenant.sharepoint.com/sites/Project01,Project Alpha Approvers,Full Control


其中 tenant 等于你的租户名称。Where tenant equals your tenant name.

  1. 将文件另存 为GroupsAndPermissions.csv。Save the file to your desktop as GroupsAndPermissions.csv.

  2. 打开记事本的新实例,然后向其中粘贴以下文本块:Open a new instance of Notepad, and paste the following text block into it:

Group,LoginName,Site
Contoso Project Leads,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Community01
Contoso Auditors,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Community01
Contoso Designers,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Community01
XT1000 Team Leads,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/TeamSite01
XT1000 Advisors,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/TeamSite01
Contoso Blog Designers,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Blog01
Contoso Blog Editors,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Blog01
Project Alpha Approvers,username@tenant.onmicrosoft.com,https://tenant.sharepoint.com/sites/Project01


其中 ,tenant 等于租户名称 ,username 等于现有用户的用户名。Where tenant equals your tenant name, and username equals the user name of an existing user.

  1. 将文件另存 为Users.csv。Save the file to your desktop as Users.csv.

  2. 打开记事本的新实例,然后向其中粘贴以下文本块:Open a new instance of Notepad, and paste the following text block into it:

Import-Csv C:\users\MyAlias\desktop\GroupsAndPermissions.csv | ForEach-Object {New-SPOSiteGroup -Group $_.Group -PermissionLevels $_.PermissionLevels -Site $_.Site}
Import-Csv C:\users\MyAlias\desktop\Users.csv | where {Add-SPOUser -Group $_.Group –LoginName $_.LoginName -Site $_.Site}


其中 MyAlias 等于当前登录的用户的用户名。Where MyAlias equals the user name of the user that is currently logged on.

  1. 将文件另存 为UsersAndGroups.ps1。Save the file to your desktop as UsersAndGroups.ps1. 这是一个简单的 Windows PowerShell 脚本。This is a simple Windows PowerShell script.

现在,您可以运行 UsersAndGroup.ps1 脚本以向多个网站集中添加用户和组。You’re now ready to run the UsersAndGroup.ps1 script to add users and groups to multiple site collections.

运行 UsersAndGroups.ps1 脚本Run UsersAndGroups.ps1 script

  1. 返回到 SharePoint Online 命令行管理程序。Return to the SharePoint Online Management Shell.
  2. 在 Windows PowerShell 提示符下键入或复制并粘贴以下行,然后按 Enter 键:At the Windows PowerShell prompt, type or copy and paste the following line, and press Enter:
Set-ExecutionPolicy Bypass

  1. 在确认提示符下,按 Y。At the confirmation prompt, press Y.

  2. 在 Windows PowerShell 提示符下键入或复制并粘贴以下内容,然后按 Enter 键:At the Windows PowerShell prompt, type or copy and paste the following, and press Enter:

c:\users\MyAlias\desktop\UsersAndGroups.ps1


其中 MyAlias 等于您的用户名。Where MyAlias equals your user name.

  1. 在继续之前,请等待提示符返回。首先,您将看到这些组在创建时的样子。然后,您将看到添加用户后的重复组列表。Wait for the prompt to return before moving on. You will first see the groups appear as they are created. Then you will see the group list repeated as users are added.

另请参阅See also

连接到 SharePoint Online PowerShellConnect to SharePoint Online PowerShell

使用 PowerShell 管理 SharePoint Online 网站用户组Manage SharePoint Online site groups with PowerShell

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

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