PowerShell – Connecting to SharePoint Online using PowerShell

What is the PowerShell?
PowerShell consists of two parts, a command-line shell and a scripting language. It started out as a framework to automate administrative tasks in Windows. PowerShell has grown into a cross-platform tool that’s used for many kinds of tasks.
There are lots of task possible to manage by using Office 365 Admin Center (Portal) but there are some tasks which are much easier to do with PowerShell.
PowerShell help to automate repetitive task.
Why PowerShell is important? Will check Real world scenario –
Imagine, you got a task, i.e. In the 1hrs time you have to create 100 Site Collections, do you think you can achieve that manually? in the very less time? Mostly Answer is No.
But if you use PowerShell, most likely it will take less than 5 mins.
In this blog series will learn all PowerShell commands.
Will start with Connect to SharePoint Online using PowerShell.
In order to connect the environment, you required SharePoint Online Management Shell.
What is SharePoint Online Management Shell? –
SharePoint Online Management Shell is a tool that contains a Windows PowerShell Module to manage your SharePoint Online subscription in the Office 365.
You can download SharePoint Online Management Shell from below URL.
https://www.microsoft.com/en-in/download/details.aspx?id=35588
Once you download, please install SharePoint Online Management Shell.
Install SharePoint Online Management Shell: –
For installing click on downloaded .exe file and as per Fig1 click on I accept the terms in the licence agreement.

After successfully installation click on finish task as per fig 2.

Run as administrator: –
Once finish the installation right click on SharePoint Online Management Shell and open this run as administrator

In order to connect, you need to be a SharePoint Online Administrator, as well as know the URL of the SharePoint Online Admin Centre, example – https://<org name>-admin.sharepoint.com
For security purpose most of company using Multifactor Authentication will check both approaches.
Will start with
Connect to SharePoint Online using PowerShell with Single Factor Authentication.
Use blow command
#Create Connection with Credential
$GetCred = get-credential
This command gets a credential object and saves it in the $ GetCred variable. When you enter the command, you are prompted for a user name and password as per Fig 4 .

Once done with connect next step is to connect to admin centre
Connect to admin centre – Connect-SPOService: –
Note – You must be a SharePoint Online administrator or Global Administrator to run the cmdlet.
Example – Connect-SPOService -Url https://contoso-admin.sharepoint.com -credential $GetCred
URL – Pass the SharePoint admin center URL i.e., shown in Fig 5.

Get-SPOSite get all site collections: –
Now connection established successfully. To verify the connection will use Get-SPOSite command this will return all our site collections. For reference, please check fig 6.

Connect to SharePoint Online using PowerShell with multi-factor authentication: –
To demonstrate multi-factor authentication, I will discount from connected environment. For disconnecting you can use Disconnect-SPOService command.

Connect-SPOService for multi-factor authentication: –
In order to connect use, Connect-SPOService command and pass the admin center URL for reference check below example
Example – Connect-SPOService -Url https://<SP Admin Center>.sharepoint.com
Once done it will ask for ID and password. After adding correct id and password you will get notification on Microsoft authentication app or Text i.e., any option set while configuring multi-factor authentication. For reference check Fig 7– Connecting to SharePoint online – multi-factor authentication.

Get-SPOSite get all Site collections using MFA
Now connection established successfully. To verify the connection will use Get-SPOSite command this will return all our site collections.
For reference, please check fig 9.

You must log in to post a comment.