Connecting SharePoint Online Using PowerShell

PowerShell is a powerful Command Line Shell Script mainly used by the administrators, and manages anything in Microsoft products. Each Microsoft product, be it Excel, SQL, Office applications, or SharePoint, has its own set of cmdlets [commands used to run any operation that can be performed using GUI and more than it].
 
Check the cmdlets that can be used in the SharePoint on-premises in my previous article.
 
But working with SharePoint online is a bit different. The prerequisites are -
  • PowerShell v3  
  • .NET Framework
  • SharePoint Module - which can be downloaded from  http://www.microsoft.com/en-us/download/details.aspx?id=35588
Once the module is downloaded, open SharePoint Online Management Shell and run the cmdlet to import the SharePoint online module cmdlets.
  1. Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking;  
Or the SharePoint Online Management Shell can be installed directly using the link - https://www.microsoft.com/en-in/download/details.aspx?id=35588
  1. Connect-SPOService -Url https://xyz.sharepoint.com -Credential [email protected]  
Note

The connecting site should be a tenant administrator site and the user should have tenant administrator access. The session will be active as an internal variable.
 
To clear out the service, run Disconnect-SPOService cmdlet which will clear the connected session.
 
For code reusability, it is better to create a function to connect with SharePoint Online adminstrator site.
  1. function ConnectSPOnlineSite() {  
  2.     param (  
  3.         $admin = "[email protected]",  
  4.         $adminSite = "https://xyz.sharepoint.com"  
  5.     )  
  6.     if ((Get-Module Microsoft.Online.SharePoint.PowerShell).Count -eq 0) {  
  7.         Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking  
  8.     }  
  9.     $cred = Get-Credential $admin  
  10.     Connect-SPOService -Url $adminSite -Credential $cred  
  11. }  
There is a small change in the cmdlets of SharePoint On-premises and SharePoint Online. In "noun-verb" part of PowerShell cmdlets, the verb has prefixed Get-SPO****.
 
Example

Get-SPOSite- gets the site collection of particular URL/identity,
Get-SPOSiteGroup- returns all site groups of one or more site collection,
Get-SPOTenant - gets SharePoint online organization properties.
 
Most of the cmdlets are available till site collection level not till the site level [SPweb level as compared to SharePoint on premises ].