PowerShell and connecting to SharePoint Online


When I was trying to connect to my online site, I was surprised to learn how different it is to PowerShell to SharePoint Online compared with SharePoint on prem.

First of all you need to download and install the SharePoint Online on your machine you are using to connect to O365. You can download these cmdlets from here. http://www.microsoft.com/en-us/download/details.aspx?id=35588. It requires PowerShell v3 or V4 installed, and SharePoint doesn’t need to be installed locally.

Once installed you will have PowerShell Online Icon in your Start area.

Opening this will give you a standard PowerShell window.

However if you are like myself, I prefer to use PowerShell ISE, but you won’t be able to use any SharePoint Online cmdlets without loading the required module. The –DisableNameChecking is to prevent a warning appearing on the screen. By default when a module that you import exports cmdlets or functions that have unapproved verbs in their name, the Windows PowerShell displays a message. This message is only a warning, and the complete module is still imported. Basically don’t worry about it.

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

Now to connect to SharePoint 365 you need to pass in your UserName, and Password. The password needs to be converted to secure string, and then you need to create a PowerShell Credential.

$userName = "myUser@sharepoint.com"
$password = "passwordAsString"
$siteCollectionUrl = "https://myonlinesite.sharepoint.com/sites/cannonfodder"
$siteAdminUrl = "https://myonlinsite-admin.sharepoint.com/"
$securePassword = ConvertTo-SecureString $password –AsPlainText –force
$O365Credential = New-Object System.Management.Automation.PsCredential($username, $securePassword)

With SharePoint 2013 online you need to connect to your tenant administration site to be able to do anything.

Connect-SPOService –url  $siteCollectionUrl –Credential $O365Credential

Don’t forget at the end of your code to close your connection.

Disconnect-SPOService

Now that you are connected you can now do something in regards to your site. There are currently a total of 30 cmdlets available to you all have “SPO” within the command. You can call up this list in PowerShell by calling

Get-Command –Module Microsoft.Online.SharePoint.Powershell

Below is what each command is for:-

Connections

Users

  • Add-SPOUser – Add a user to an existing Site Collection Site Group.
  • Get-SPOUser – Get an existing user.
  • Remove-SPOUser – Remove an existing user from the Site Collection or from an existing Site Collection Group.
  • Set-SPOUser – Set whether an existing Site Collection user is a Site Collection administrator or not.
  • Get-SPOExternalUser – Returns external users from the tenant’s folder.
  • Remove-SPOExternalUser – Removes a collection of external users from the tenancy’s folder.

Site Groups

Site Collections

  • Get-SPOSite – Retrieve an existing Site Collection.
  • New-SPOSite – Create a new Site Collection.
  • Remove-SPOSite – Move an existing Site Collection to the recycle bin.
  • Repair-SPOSite – If any failed Site Collection scoped health check rules can perform an automatic repair then initiate the repair.
  • Set-SPOSite – Set the Owner, Title, Storage Quota, Storage Quota Warning Level, Resource Quota, Resource Quota Warning Level, Locale ID, and/or whether the Site Collection allows Self Service Upgrade.
  • Test-SPOSite – Run all Site Collection health check rules against the specified Site Collection.
  • Upgrade-SPOSite – Upgrade the Site Collection. This can do a build-to-build (e.g., RTM to SP1) upgrade or a version-to-version (e.g., 2010 to 2013) upgrade. Use the -VersionUpgrade parameter for a version-to-version upgrade.
  • Get-SPODeletedSite – Get a Site Collection from the recycle bin.
  • Remove-SPODeletedSite – Remove a Site Collection from the recycle bin (permanently deletes it).
  • Restore-SPODeletedSite – Restores an item from the recycle bin.
  • Request-SPOUpgradeEvaluationSite  – Creates a copy of the specified Site Collection and performs an upgrade on that copy.
  • Get-SPOWebTemplate – Get a list of all available web templates.

Tenants

  • Get-SPOTenant – Retrieves information about the subscription tenant. This includes the Storage Quota size, Storage Quota Allocated (used), Resource Quota size, Resource Quota Allocated (used), Compatibility Range (14-14, 14-15, or 15-15), whether External Services are enabled, and the No Access Redirect URL.
  • Get-SPOTenantLogEntry – Retrieves company logs (as of B2 only BCS logs are available).
  • Get-SPOTenantLogLastAvailableTimeInUtc – Returns the time when the logs are collected.
  • Set-SPOTenant – Sets the Minimum and Maximum Compatibility Level, whether External Services are enabled, and the No Access Redirect URL.

Apps

Below is the full script which connects and get the site title of the Site Collection.

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
$userName = "myUser@sharepoint.com"
$password = "passwordAsString"
$siteCollectionUrl = "https://myonlinesite.sharepoint.com/sites/cannonfodder"
$siteAdminUrl = "https://myonlinsite-admin.sharepoint.com/"
$securePassword = ConvertTo-SecureString $password –AsPlainText –force
$O365Credential = New-Object System.Management.Automation.PsCredential($username, $securePassword)
Connect-SPOService –url  $siteCollectionUrl –Credential $O365Credential
$site = Get-SPOSite $siteCollectionUrl
write-host "Title: " $site.Title "Url: "$site.Url"
Disconnect-SPOService