How to Connect to SharePoint Online using PowerShell and PnP PowerShell?

Are you looking to connect to SharePoint Online using PowerShell? I will explain here, in detail, how to connect to SharePoint Online using PowerShell as well as PnP PowerShell.

You will find this tutorial as one of the simplest tutorials that will help you to connect to SharePoint online using PowerShell. I will explain in the simplest way possible.

How to Connect to SharePoint Online Using PowerShell

If you are an experienced professional, you can also use PowerShell to manage your SharePoint Online. I will show you two ways to connect to a SharePoint Online site using PowerShell.

  • Using SharePoint Online Management Shell
  • Using PnP PowerShell

1. Using SharePoint Online Management Shell

To work with SharePoint Online using PowerShell, you first need to install SharePoint Online Management Shell. There are two ways you can install it.

  • The first approach you can download and install the SharePoint Online management shell from here.

On the downloads page, you can see a download option like below, and then you can follow the steps to install it.

install sharepoint online powershell module

Just follow the instructions and install it; I am 100% sure you will not face any issues while installing it.

  • In the other approach, you can run the below cmdlet to install the module from the PowerShell gallery.
Install-Module -Name Microsoft.Online.SharePoint.PowerShell

If you do not have the admin privileges, then you can install it for the current user by running the below commands:

Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser

To check whether the SharePoint Online management shell is installed or not, you can use the below cmdlets:

Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select Name,Version

You can see the name and version in the screenshot below. I executed the script using VS code:

how to connect to sharepoint online powershell

If you have already installed it, then you can update it to the latest version.

To update to the latest version of the SharePoint Online management shell, run the below cmdlets:

Update-Module -Name Microsoft.Online.SharePoint.PowerShell

After the installation is successful, you can connect to SharePoint Online using the Connect-SPOService cmdlet.

NOTE: You need to be a SharePoint Online Administrator or Global Administrator to connect to SharePoint Online from SharePoint Online Management Shell (PowerShell).

Now, to connect to SharePoint Online from PowerShell, run the below command.

Connect-SPOService -Url https://<tenantname>-admin.sharepoint.com -Credential admin@<tenantname>.com

Example:

Connect-SPOService -Url https://szg52-admin.sharepoint.com -Credential bijay@szg52.onmicrosoft.com

Once you run the above PowerShell cmdlet, it will ask you to enter the password for the user like below. Once you enter the password, you will be connected to the SharePoint site from PowerShell.

connect to sharepoint online powershell

You can also use the below cmdlet to connect to SharePoint Online using PowerShell.

Connect-SPOService -Url https://<tenantname>-admin.sharepoint.com -Credential (Get-Credential)

In this case, it will ask you to enter both the username and password.

Once the connection is successful, you can run the below command to get all the sites from the SharePoint Online tenant.

Get-SPOSite

You can see the output in the screenshot below:

connect sharepoint online powershell

This is how to connect to SharePoint Online from PowerShell using SharePoint Online Management Shell.

If you want to pass the credentials in the PowerShell script itself, then you can write like the below PowerShell script.

$siteURL="https://szg52-admin.sharepoint.com"
$userName="bijay@szg52.onmicrosoft.com"
$password = "Mypassword@1"

$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $(convertto-securestring $password -asplaintext -force)
Connect-SPOService -Url $siteURL -Credential $credentials

2. Using PnP PowerShell

Now, I will show you how to connect to SharePoint Online using PnP PowerShell.

PnP PowerShell, also known as “Patterns and Practices PowerShell,” is an extension of PowerShell that provides a library of PowerShell commands (cmdlets) that are designed to perform complex provisioning and artifact management actions towards SharePoint.

By using PnP PowerShell, you can work with various Microsoft products like SharePoint Online, Microsoft Teams, etc.

You can follow the below three steps to connect to SharePoint Online from PnP PowerShell.

Step-1:

First, you need to install PnP PowerShell by using the below command.

Install-Module PnP.PowerShell
Or
Install-Module PnP.PowerShell -Scope CurrentUser

Note: Make sure you have PowerShell 7.2 or later on your machine.

You can see the output in the screenshot below:

how to install pnp powershell for sharepoint online

Step-2:

Next, run the below cmdlet to authenticate to your tenant.

Register-PnPManagementShellAccess

Step-3:

Once the installation is successful, you can run the below command to connect to the SharePoint Online tenant.

Connect-PnPOnline [tenant].sharepoint.com -Credentials (Get-Credential)
Example:
Connect-PnPOnline szg52.sharepoint.com -Credentials (Get-Credential)

To verify the installation, you can run the below command.

Get-Command -Module PnP.Powershell

You can see the output in the screenshot below:

pnp powershell for sharepoint online

Here are some other cmdlets to connect to SharePoint Online using PnP PowerShell.

Once you installed PnP PowerShell, then you can run the below cmdlet to connect to a SharePoint Online.

Connect-PnPOnline -Url https://szg52.sharepoint.com

It will ask you to enter the username and password.

Instead of connecting to a SharePoint Online tenant, if you want to connect to a SharePoint Online site, you can run the below PnP PowerShell script:

Connect-PnPOnline -Url https://<tenantname>.sharepoint.com/sites/siteName -Credentials (Get-Credential)

Example:

Connect-PnPOnline -Url https://szg52.sharepoint.com/sites/PowerShellFAQs -Credentials (Get-Credential)

It will ask you to enter the username and password.

You can also use the -UseWebLogin parameter to connect to SharePoint Online using the Connect-PnPOnline cmdlet.

Connect-PnPOnline -Url https://szg52.sharepoint.com -UseWebLogin

If you want to provide the username and password to connect to SharePoint using PnP PowerShell, you can do like below:

$siteURL="https://szg52.sharepoint.com"
$userName="bijay@szg52.onmicrosoft.com"
$password = "Mypassword@1"
 
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $securePassword

Connect-PnPOnline -Url $siteURL -Credentials $credentials

Note: You need to pass the password as a secure string.

This is how to connect to SharePoint Online using PnP PowerShell.

I hope you have a complete idea of how to connect to SharePoint Online using PowerShell and PnP PowerShell. Working with SharePoint Online management shell or PnP PowerShell is really easy. You can check out our PowerShell SharePoint tutorials.

You may also like: