Export (0) Print
Expand All

Creating sites and adding users using Windows PowerShell

 

Topic Last Modified: 2014-09-09

Summary: Use Windows PowerShell to Manage Office 365 using Windows PowerShell cmdlets, scripts, and batch processes.

When you use Windows PowerShell 3.0 to create sites and add users, you can quickly and repeatedly perform tasks much faster than you can in the administration user interface. You can also perform tasks that are not possible to perform in the administration user interface.

Before we get started using Windows PowerShell to create sites and add users, make sure that the SharePoint Online Management Shell and its prerequisites are installed.

For information about how to install the SharePoint Online Management Shell, see Set up the SharePoint Online Management Shell Windows PowerShell environment.

Create multiple sites using Windows 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. This process allows you to create a single file and run a single Windows PowerShell command that uses that file. 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. First you’ll create a .csv file, and then you’ll reference that .csv file using Windows PowerShell, which will use its contents to create the sites.

The Windows 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. The Windows 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.

Create a .csv file
  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
    
    

    Where:

    • <tenant> is the name of your tenant. You can press Ctrl+H when you use Notepad to bulk replace faster.

    • <owner> is the user name of the user on your tenant to whom you want to grant the role of primary site collection administrator.

  2. Save the file on your desktop as SiteCollections.csv.

    TipTip:
    Before you use this or any other .csv or Windows PowerShell script file, it is good practice to make sure that there are no extraneous or nonprinting characters. 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.
Run the Windows PowerShell command
  1. At the Windows PowerShell prompt, type or copy and paste the following cmdlet, 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}
    

    Where <MyAlias> equals your user alias.

  2. Wait for the Windows PowerShell prompt to reappear. It might take a minute or two.

  3. At the Windows PowerShell prompt, type or copy and paste the following cmdlet, and press Enter:

    Get-SPOSite -Detailed | Format-Table -AutoSize
    
  4. Note the new site collections in the list. You should see the following site collections: contosotest, TeamSite01, Blog01, and Project01.

  5. That’s it. You’ve created multiple site collections using the .csv file you created and a single Windows PowerShell cmdlet. You’re now ready to create and assign users to these sites.

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.

The following procedures assume that you successfully created the site collections contosotest, TeamSite01, Blog01, and Project01.

Create .csv and .ps1 files
  1. Open Notepad, and paste the following text block into it:

    Site,Group,PermissionLevels
    https://<tenant>.sharepoint.com/sites/contosotest,Contoso Project Leads,Full Control
    https://<tenant>.sharepoint.com/sites/contosotest,Contoso Auditors,View Only
    https://<tenant>.sharepoint.com/sites/contosotest,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
    
    

    Where <tenant> equals your tenant name.

  2. Save the file to your desktop as GroupsAndPermissions.csv.

  3. 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/contosotest
    Contoso Auditors,<username>@<tenant>.onmicrosoft.com,https://<tenant>.sharepoint.com/sites/contosotest
    Contoso Designers,<username>@<tenant>.onmicrosoft.com,https://<tenant>.sharepoint.com/sites/contosotest
    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
    
    

    Where:

    • <tenant> equals your tenant name.

    • <username> equals the user name of an existing user.

  4. Save the file to your desktop as Users.csv.

  5. 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}
    

    Where <MyAlias> equals the user name of the user that is currently logged on.

  6. Save the file to your desktop as UsersAndGroups.ps1. This is a simple Windows PowerShell script.

  7. You’re now ready to run the UsersAndGroup.ps1 script to add users and groups to multiple site collections.

Run UsersAndGroups.ps1 script
  1. Return to the SharePoint Online Management Shell.

  2. At the Windows PowerShell prompt, type or copy and paste the following line, and press Enter:

    Set-ExecutionPolicy Bypass
    
  3. At the confirmation prompt, press Y.

  4. At the Windows PowerShell prompt, type or copy and paste the following, and press Enter:

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

    Where <MyAlias> equals your user name.

  5. 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.

  6. That’s it. You’ve created new sites and users and added the users to those sites using Windows PowerShell.

 
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft