PowerShell Connect to SharePoint Online

Procedure

Start Windows PowerShell as an administrator.

  1. Go to  Start in your computer then enter PowerShell in the Search box.You will see Windows PowerShell in the results. Right-click Windows PowerShell  then click Run as administrator.

  2. If the User Account Control dialog box appears, click Yes to verify that you want to run Windows PowerShell under administrator credentials.

If you don't, you're going to get an error message as in the following:

Create a credentials object before executing the script because it will provide a safe and secure way to pass your user name and password to Office 365.

Type the following command from the Windows PowerShell prompt and press Enter. It will open the dialog box window and enter your Office 365 user name and password.

  1. Script: $credential = Get-Credential  

 



Connect to SharePoint Online

Before connecting to SharePoint Online, we need to supply two pieces of information:

  • Credentials

  • URL of the SharePoint admin site

We need to import the correct Windows PowerShell module before we connect to SharePoint. Type this command from the Windows PowerShell prompt and then press Enter.

  1. Script: Import-Module Microsoft.Online.SharePoint.PowerShell  
The Credentials object we have already created. 

Identify the admin site URL as in the following:

  1. Add your domain name.

      a. For example, my domain is gowtham.onmicrosoft.com then your domain host name is the gowtham.
  2. Add a hyphen (-) followed by -admin.sharepoint.com.

My admin site URL: https://thegowtham-admin.sharepoint.com

After constructing the admin URL, connect to the SharePoint service using PowerShell as in the following:

  1. Script:Connect-SPOService -Url https://thegowtham-admin.sharepoint.com -credential $credential  
Verify the script: Enter the following script and check the output. 

  1. Script:Get-SPOSite  

 



You should get back a list of all your SharePoint Online sites.

The final step is very important. To close the SharePoint Online session, enter this command:

  1. Script Disconnect-SPOService  
Combined Scripts
  1. $credential = Get-Credential  
  2. Import-Module Microsoft.Online.SharePoint.PowerShell  
  3. Connect-SPOService -Url https://thegowtham-admin.sharepoint.com -credential $credential  
  4. Get-SPOSite  
  5. Disconnect-SPOService  
Reference: Technet Microsoft