Getting Started With PowerShell For SharePoint Online

PowerShell is a very versatile scripting tool, and its importance and usage in SharePoint on premise in very well known. Thanks to Microsoft, we now have some of the commands available for the SharePoint online version as well.

This article explains how to set up your dev environment, to use the PowerShell commands for SharePoint online.

  1. You need to have windows PowerShell 3.0 to start with.

    a. To check the version use the command $PSVersionTable.PSVersion.

  2. Post this we need to install the SharePoint Online Management Shell,

    a. Download link.
    b. The msi is about 2MB, and is a one click install.

    sharepoint

  3. Please ensure that the Windows PowerShell console is closed during the installation.

  4. Start the SharePoint Online Management Console from all programs.

    Note: Ensure to run the console as Administrator else the Import Module command may fail.

    admin

  5. Now for connecting to the site use the command Connect-SPOService,

    The best part of using PowerShell is, you just type the command and hit enter. PowerShell takes care of the rest, by prompting for the required inputs.

  6. Pass the site url.

  7. Enter the admin credentials. (In Email id format:- Eg- [email protected])
    Wohoo! In ideal cases and as per the MSDN site this must connect you to SharePoint online

    But if you are still not able to connect, then this is because when trying to administer SharePoint Online (O365) via PowerShell through a proxy server the Connect-SPOService cmdlet connections fail.

    We need to do a small trick with the Web proxy credentials. Re-run the Connect-SPOService command after executing the following command.
    1. $cred = [System.Net.CredentialCache]::DefaultCredentials  
    2. [System.Net.WebRequest]::DefaultWebProxy.Credentials = $cred  
  8. Let’s re-perform steps 5, 6 & 7 and we are connected to the site. But, what looks so straight is never that easy. Or you can see I wasn’t lucky enough. The issue is with the url you enter in the connection command.

    Which is typically your normal SharePoint online url, but then with a -admin after it, such as:
    https://mysharepointsite-admin.sharepoint.com (assuming that your sharepoint online site is https://mysharepointsite.sharepoint.com). So remember to use the url with –admin in it.

Now you are connected and you can test by running the command Get-SPOSite. It must display all the site collections.

I have encountered some errors, the following is the table with the details of error and possible solution.

  Error Message Resolution
1 import-module the specified module 'microsoft.online.sharepoint.powershell' was not loaded Run the console in Admin mode. Step #4
2 Connect-SPOService: Could not connect to SharePoint Online. Check Step #7. Need to run additional commands.
3 Connect-SPOService: Current site is not a tenant administration site. Check Step #8. The site url must have a –admin.