使用 PowerShell 建立 SharePoint 網站並新增使用者

本文適用於 Microsoft 365 企業版和 Office 365 企業版。

當您使用適用於 Microsoft 365 的 PowerShell 來建立 SharePoint 網站並新增使用者時,可以比在 Microsoft 365 系統管理中心 中更快且重複地執行工作。 您也可以執行無法在 Microsoft 365 系統管理中心 中執行的工作。

線上到 SharePoint

本文中的程式會要求您連線到 SharePoint。 如需指示,請 參閱連線到 SharePoint PowerShell

步驟 1:使用 PowerShell 建立新的網站集合

使用 PowerShell 建立多個網站,以及您使用提供的範例程式代碼和記事本建立的 .csv 檔案。 在此程式中,您要將方括弧中顯示的佔位符資訊取代為您自己的網站和租使用者特定資訊。 此程式可讓您建立單一檔案,並執行使用該檔案的單一PowerShell命令。 這可讓動作可重複且可攜,並消除許多可能來自在 SharePoint 管理命令介面中輸入長命令的錯誤。 此程式有兩個部分。 首先,您會建立 .csv 檔案,然後使用 PowerShell 參考該 .csv 檔案,其會使用其內容來建立網站。

PowerShell Cmdlet 會匯入 .csv 檔案,並將它傳送至大括號內的迴圈,以將檔案的開頭行讀取為數據行標頭。 PowerShell Cmdlet 接著會逐一查看其餘記錄、為每一筆記錄建立新的網站集合,並根據數據行標頭指派網站集合的屬性。

建立 .csv 檔案

注意事項

資源配額參數只適用於傳統網站。 如果您在新式網站上使用此參數,您可能會收到警告訊息,指出它已被取代。

  1. 開啟記事本,並將下列文字塊貼到其中:

    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 是租用戶的名稱,而 擁有者 是您想要授與主要網站管理員角色之租使用者上的用戶名稱。

    (當您使用記事本大量取代 faster 時,可以按 Ctrl+H。)

  2. 將檔案儲存在桌面 上,SiteCollections.csv

提示

在您使用這個或任何其他 .csv 或 Windows PowerShell 腳本檔案之前,最好先確定沒有無關或非列印的字元。 在 Word 中開啟檔案,然後在功能區中,按兩下段落圖示以顯示非列印字元。 不應該有無關的非列印字元。 例如,除了檔尾的最後一個段落標記之外,應該沒有段落標記。

執行 Windows PowerShell 命令

  1. 在 Windows PowerShell 提示字元中,輸入或複製並貼上下列命令,然後按 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 等於您的用戶別名

  2. 等候 Windows PowerShell 提示重新出現。 可能需要一兩分鐘的時間。

  3. 在 Windows PowerShell 提示字元中,輸入或複製並貼上下列 Cmdlet,然後按 Enter:

    Get-SPOSite -Detailed | Format-Table -AutoSize
    
  4. 請注意清單中的新網站集合。 使用我們的範例 CSV 檔案,您會看到下列網站集合: TeamSite01Blog01Project01Community01

這樣就完成了。 您已使用您建立的 .csv 檔案和單一 Windows PowerShell 命令來建立多個網站集合。 您現在已準備好建立使用者,並將使用者指派給這些網站。

步驟 2:新增使用者和群組

現在您要建立使用者,並將其新增至網站集合群組。 您將使用 .csv 檔案來大量上傳新的群組和使用者。

下列程式會繼續使用範例網站 TeamSite01、Blog01、Project01 和 Community01。

建立 .csv 和 .ps1 檔案

  1. 開啟記事本,並將下列文字塊貼到其中:

    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
    

    其中 使用者等於您的租用戶名稱

  2. 將檔案儲存到您的桌 面,GroupsAndPermissions.csv

  3. 開啟記事本的新實例,並將下列文本塊貼到其中:

    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
    

    其中 使用者等於您的租用戶名稱,而 用戶名稱 等於現有使用者的用戶名稱。

  4. 將檔案儲存到您的桌 面,Users.csv

  5. 開啟記事本的新實例,並將下列文本塊貼到其中:

    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 等於目前登入之用戶的用戶名稱。

  6. 將檔案儲存到您的桌面,UsersAndGroups.ps1,這是簡單的 Windows PowerShell 腳本。

您現在已準備好執行 UsersAndGroup.ps1 腳本,將使用者和群組新增至多個網站集合。

執行 UsersAndGroups.ps1 腳本

  1. 返回 SharePoint 管理命令介面。

  2. 在 Windows PowerShell 命令提示字元中,輸入或複製並貼上下列行,然後按 Enter:

    Set-ExecutionPolicy Bypass
    
  3. 在確認提示字元中,按 Y

  4. 在 Windows PowerShell 命令提示字元中,輸入或複製並貼上下列內容,然後按 Enter:

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

    其中 MyAlias 等於您的用戶名稱

  5. 在繼續之前,等候提示傳回。 您會先看到群組在建立時出現。 接著,您會看到群組清單在新增用戶時重複。

另請參閱

線上到 SharePoint PowerShell

使用 PowerShell 管理 SharePoint 網站群組

使用 PowerShell 管理 Microsoft 365

開始使用適用於 Microsoft 365 的 PowerShell