In this post I describe how to connect to SharePoint 2013 Online using PowerShell and how to create and delete team sites there.
Download SharePoint Online Management Shell
It is recommended that the SharePoint Online Management Shell is installed on your system. The preview edition can be downloaded from here:
Connect to Service
First of all, it is neccessary to establish a connection to SharePoint Online. Important her is to pass in the URL to the admin site collection. Other URLs won’t work. This command will establish the connection, the password will be asked by a dialogue as you run the command:
Connect-SPOService -Url https://yourdomain.sharepoint.com -Credential firstname.lastname@example.org
The following commands allows to bypass the dialogue by passing in of a password within the script:
$credentials = New-Object -TypeName System.Management.Au tomation.PSCredential -ArgumentList "email@example.com. com", $(ConvertTo-SecureString "yourpassword" -AsPlainText -Force) Connect-SPOService -Url https://yourdomain-admin.sharepoint.com -Credential $credentials
Now there is a connection established with SharePoint Online. You can test your connection by getting information about your site using this command:
Get-SPOSite -Identity https://yourdomain.sharepoint.com
Create a new Site
The following command will create a new teamsite under the path “/sites/teamsitecollection2”:
New-SPOSite -Url https://yourdomain.sharepoint.com/sites/teamsitecollection2 -Owner firstname.lastname@example.org -StorageQuota 500 -ResourceQuota 50 -Title "Created by PowerShell" -Template STS#0
The script may take some time to finish. To continue with your script without waiting for SharePoint Online, add the parameter “-NoWait”.
The creation status can be watched in the admin area of SharePoint Online:
Delete a Site
With the following the site created above will be deleted and moved to the recycle bin:
Remove-SPOSite -Identity https://yourdomain.sharepoint.com/sites/teamsitecollection2
The following command removes the site collection from the recycle bin:
Remove-SPODeletedSite -Identity https://yourdomain.sharepoint.com/sites/teamsitecollection2
Is there a use case for this?
In my opinion these snippets will help in an environment where On-Premise and Cloud shall be used in hybrid mode. If a team site needs to be accessed by external users then it is neccessary to create this site in SharePoint Online.
Windows PowerShell for SharePoint Online reference