How to connect to SharePoint Online using PowerShell?

connect powershell to sharepoint online

Before you perform any kind of an administrative task in your SharePoint Online, you need to connect to your organization. Mind that connecting to SharePoint Online might be useful not only when you want to manage your SharePoint sites and permissions, but also when you want to run some scripts on Office 365 groups. This article shows you how to take the first step to manage your online resources by connecting to your SharePoint organization with PowerShell.

How to connect to SharePoint Online

There are a few different approaches which allow you to get access to your resources in SharePoint Online. While all methods require you to use the Conect-SPOService cmdlet, they each offer a slightly different experience.

Method 1 – manual connection

The easiest method to connect your PS console to SharePoint Online is to run the following cmdlet:

Connect-SPOService -url https://*org-name*-admin.sharepoint.com

Mind to replace *org-name* with the actual name of your SharePoint organization. After you hit enter, you will see a familiar Office 365 login page:

Sign in to Office 365

Simply enter your credentials and the PowerShell console will connect to SharePoint Online. This method has one clear advantage – it works even when your account has MFA enabled. On the other hand, most people use PowerShell not to deal with the popups and additional windows and would rather avoid unnecessary popups. You will find this method especially troublesome if you decide to connect PowerShell to another module (like Exchange Online). Fortunately, there are different methods for accessing SharePoint with your console.

Method 2 – remember your credentials for the Session

If you do not wish to reenter your credentials each time you want to reconnect to your organization (or connect to another module), this method is optimal for you:

$creds = Get-Credential -UserName *yourUPN* -Message "Enter your Office 365 credentials";
Connect-SPOService -url https://*org-name*-admin.sharepoint.com -Credential $creds

The Get-Credential cmdlet will cause a standard PowerShell sign-in box to pop up. The -UserName attribute allows you to automatically fill in the login field. The credentials will be remembered as long as you keep your PowerShell console open and don’t overwrite the $creds variable. Mind that you can use your saved credentials to log in to other Office 365 services, for example:

Connect-AzureAD -Credential $creds
Connect-MSOLService -Credential $creds

Method 3 – connect without any sign in boxes

This final method allows you to login without any pop-ups or additional boxes – you perform each task in the console.

$login = *yourUPN*;
$pwd = *password*;
$pwd = ConvertTo-SecureString $pwd -AsPlainText -Force;
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $login,$pwd;
Connect-SPOService -url https://*org-name*-admin.sharepoint.com -Credential $credentials;

Some optimization-oriented admins are tempted to automatize the login procedure by saving their credentials in the script files. Unfortunately, this exposes login data in the script. It is not recommended, it is not safe, it is not good practice, but it allows automation at its finest.

Test your connection

No matter which method you choose, you will not get any “connection established” notification. Although no error usually means that you have connected successfully, you should make sure. To check if you have been successful connecting to SharePoint Online, you can run a simple cmdlet:

Get-SPOSite

You should see the list of your SharePoint Online sites. If that’s the case, you should be able to perform any administrative task you have enough permissions to do.

Common problems and their solutions

There are a few problems you might run into while trying to connect with SharePoint Online.

Connect-SPOService : The remote server returned an error: (401) Unauthorized.

This error appears when the user does not have permissions to connect to the Office 365 organization via PowerShell. The remedy is simple – have your administrator run Set-User <your_identity>  -RemotePowerShellEnabled $true

Get-SPOSite : No connection available. Use Connect-SPOService before running this CmdLet.

It seems that you have failed to successfully connect to your SharePoint Online organization. Go through the connection procedure once more and make sure you follow each step closely.

Connect-SPOService : The term ‘Connect-SPOService’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

You need to download the SharePoint Online module for PowerShell. Run:

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

to confirm if you have the required module (and also if you need an update), and use:

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

To fix the problem.

Connect-SPOService : The sign-in name or password does not match one in the Microsoft account system.

There are two possible reasons for this error – either you have made a mistake while inserting your credentials, or you have a multifactor authentication enabled (MFA). If it is due to the MFA, you will need to remove the -Credential parameter while connecting to SharePoint Online and enter your credentials with the traditional, login page experience.

Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.

Run Get-ExecutionPolicy to see what is your Execution Policy. If it is restricted, use Set-ExecutionPolicy RemoteSigned to let yourself run the required scripts.

Summary

Accessing your SharePoint Online via PowerShell is an easy first step you have to take before you experience a whole new level of SharePoint management. Stay tuned for more detailed articles on how to manage SharePoint Online.

Useful links:

Tools for Microsoft 365

Recommended articles

How to migrate from Exchange Server 2016/2019 to Microsoft 365

How to migrate from Exchange Server 2016/2019 to Microsoft 365

Migrating Exchange data to the cloud is not rocket science – explore your options and launch the migration stress-free. As you may already know, Exchange Server 2016 and 2019 have reached end of life and are no longer officially supported by Microsoft. If your organization still uses either of these platforms to manage email, contacts, calendars, and tasks, keep in mind that: Your environment may become vulnerable to newly discovered security threats – Microsoft will no longer provide security updates for Exchange 2016 and 2019 (unless you’ve enrolled in the Extended Security Update program, which ends in October 2026). Your emails may get blocked – Microsoft has started to throttle and block emails sent from unsupported Exchange Server versions to Exchange Online (as I covered in this article). That said, migrating to a supported platform is now the only viable long-term option for keeping your organization’s email environment secure, supported, and fully operational. If your organization wants or needs to keep things on‑premises (and continue using Microsoft’s solutions for that), upgrading to Exchange Server Subscription Edition (SE) is the only path forward. But given Microsoft’s clear preference for its cloud services – evident in the faster rollout of new features and the many security capabilities available exclusively in Microsoft 365 (Office 365) – now is a great time to leave your on‑prem environment behind and migrate to Exchange Online as part of Microsoft 365. While switching over to a new platform might seem like a rough ride, I’ll show you some easy ways to follow when migrating mailboxes from Exchange Server 2016/2019 to Microsoft 365. How to prepare for email migration to Microsoft 365 Before you start the migration process, you need to make sure your environment is ready for the move. For this purpose, you can use this guide in the Microsoft 365 admin center – it will help you connect your organization to Microsoft 365 and integrate your existing user accounts with Microsoft Entra ID. Microsoft also recommends completing the steps below: Set up an SPF record to determine valid email sources for your organization’s Microsoft 365 domain. Set up the Exchange Online Protection service as a means of protection against spam and malware. If you’re behind on updates, make sure to install the latest Cumulative Update (CU). And here is my quick, less obvious Microsoft 365 migration checklist: Verify if your software will work in Microsoft 365 (especially when it comes to server software). Microsoft 365 migration might be the time you learn that there is crucial legacy software that half the company uses and which is hard to replace. Encourage the whole company to clean up projects. It’s much easier to do this before the migration and start fresh. Gather as much information about your on‑premises environment as possible. For example, you might need to recreate access roles and permissions from scratch in the cloud or set up mail flow rules. Without prior research, it will be much more difficult. Verify if you need to migrate service accounts. There can be a lot of them on‑premises and in most cases, you won’t need them after the move. Review mailbox size limits in Exchange Online before migration to see which licenses you’ll need and whic
How to connect and remotely manage Microsoft 365 with PowerShell

How to connect and remotely manage Microsoft 365 with PowerShell

Microsoft 365 web interface was designed to make it easier to manage your tenant right down to its administrative bowels. On the one hand it really is quick and simple to navigate, on the other it definitely lacks some advanced configuration options so loved by sysadmins. Luckily there is the mighty PowerShell coming to the rescue! You should already know its potential, which can also be utilized in Microsoft 365. Find out how.
New-ComplianceSearch: how to use the newer version of Search-Mailbox

New-ComplianceSearch: how to use the newer version of Search-Mailbox

Microsoft retired the Search-Mailbox cmdlet – now what? Discover how to use New-ComplianceSearch, its key advantages and how to make the switch seamlessly.

Comments

  1. Hello, I’m getting the error message below. I’m in an on-prem sharepoint Farm and I have downloaded SharePoint 2019 Management Shell as well as Windows Powershell.

    Connect-SPOSevice : The term ‘Connect-SPOSevice’ is not recognized as the name of a cmdlet, function, script file, or
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
    again.
    At line:1 char:1
    + Connect-SPOSevice
    + ~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Connect-SPOSevice:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    • avatar
      Adam the 32-bit Aardvark says:

      Hello,
      There are two problems here:
      1. There’s a typo in your “Connect-SPOService”.
      2. The Connect-SPOService applies to SharePoint Online, not SharePoint Server 2019.

      To learn more about PowerShell in on-prem SharePoint Server, check out this resource from Microsoft.

  2. I am getting the error as:

    Connect-SPOService : The sign-in name or password does not match one in the Microsoft account system.
    At line:1 char:1
    + Connect-SPOService
    + ~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Connect-SPOService], IdcrlException
    + FullyQualifiedErrorId : Microsoft.SharePoint.Client.IdcrlException,Microsoft.Online.SharePoint.PowerShell.Connec
    tSPOService

    Here I am giving the credentials correctly, but still it is showing the error. What should I do?

    • avatar
      Adam the 32-bit Aardvark says:

      Then most probably you have MFA enabled in your tenant. Have you tried logging in without the -Credential parameter? It should open a standard Microsoft 365 Sign in window.

  3. Hi – I am a newbie on powershell for SharePoint online, what I was trying to do is to get all subsites and lists with item counts. I have a script saved but not sure what cmdlets to use to run the script. Really dot appreciate any info you can provide. thank you in advanced.

    • avatar
      Adam the 32-bit Aardvark says:

      Hi MA,
      If you have a script, you do not have to use any additional cmdlets, simply run the script.
      To run the script, change the directory to where the script is and run it. For example:
      cd “C:\scripts”
      .\”the script you downloaded”.ps1
      If you need to make any changes to the script file, they would normally be mentioned in the script documentation (where you downloaded it from) or in the script itself, as comments. It is hard to say more, not knowing what kind of script you use.

  4. Hi
    Thanks for the post..
    I am getting Connect-SPOService : Root element is missing.
    when trying to connect.

    Can you tell me what is the issue ..

    Thanks
    PCM

    • avatar
      Adam the 32-bit Aardvark says:

      The Root element is missing error usually occurs when you have a blank variable or attribute in your code. Which of the connection methods are you using?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

CodeTwo sp. z o.o. sp. k. is a controller of your personal data.
See our Privacy Policy to learn more.