Skip to the content

How to connect PowerShell to SharePoint Online


PowerShell is a powerful mass automation tool designed to help a company's IT department manipulate and coordinate the underlying operations of an operating system, application, or Web page. How?

With the famous cmdlets and their preconfigured modules.

Prominent among them is the one dedicated to SharePoint.

Let's find out all the details together.

PowerShell: what it is and what is it for

Automation and scripting: that's how we could sum up, in a nutshell, what PowerShell is.

This platform was created by Microsoft to help IT teams automate processes inherent in a company's IT resources using custom scripts and commands.

Thus, it is a tool directed at experienced programming users who do not want to waste time creating automation manually, one by one, and always starting from scratch.

Instead, they want to make tasks such as installing apps, configuring servers, or managing SharePoint site collections in bulk automatic.

If you also share this goal, or if you are simply curious about what PowerShell can do for your business, you are in the article.

What is intranet.ai?

intranet.ai is the ready-to-use corporate intranet, designed to reduce costs and implementation times, thus creating a collaborative and modern digital work environment:

  • Initial price of 3,650 €/year for a complete intranet
  • 50+ applications, including advanced search, document management system, push notifications via Teams, personal profile, and FAQ pages
  • 100% integrated with SharePoint Online and Microsoft 365

 


Before we look at what the main features of the product are, we want to make a small clarification, which may seem obvious to some of you.

We mean that PowerShell has nothing to do with Power Automate, if only to create automation.

PowerShell is characterized by a dual nature, involving being a scripting language as well as an object-oriented automation engine.

It is thus a tool that is generally used by system admins to manage servers, network, apps, sites, and enterprise Web services, making the processes on which these resources rely automatically.

For its part, Power Automate allows you to create cloud workflows without resorting to writing code and aimed at integrating the functionality of Microsoft tools, as well as third-party tools.

So, you could build workflows capable of collecting your email attachments and then storing them in a given SharePoint library without ever having to access Outlook.

Another example would be to make it automatic to send notifications to your Teams groups and channels whenever a post is published to a SharePoint site or social page.

Or again, you could create a flow to manage the approval of new documents uploaded to the archive.

As you can see, Power Automate delivers the best surprises when it comes to automating sites and, more importantly, SharePoint lists and libraries.

But we will go no further with the details here.

 

To explore this topic further, we recommend reading our article on
workflows for SharePoint Online.

 

After this brief but necessary introduction, we can delve into the intricacies of PowerShell to find out what other features differentiate it from common automation tools and thus make it an attractive asset with which to equip your IT team.

What are the main features of PowerShell

As anticipated in the last chapter, PowerShell constitutes both a scripting language and a platform intended for the automation of operating systems, especially, but also of applications and Web pages.

Regarding operating systems, it should be noted that a major advantage of PowerShell is that it can adapt to major platforms (Windows, Linux, and macOS) without any problems.

To achieve its goal, it leverages the .NET framework, which allows it to provide users with several ways in which they can create their automations. These include:

  • Cmdlet.
  • Scripts.
  • Executable files.
  • Standard .NET classes.

A brief parenthesis must be opened for the first ones.

Indeed, cmdlets (pronounced "command-lets") represent the most characteristic elements of PowerShell, and the most commonly used by users.

Specifically, they are lightweight and specialized commands, since each of them adheres to a simple (Verb-Substantive) and standardized syntax that refers to a single operation. Some examples might be "Get-Location," "Move-Item," and "Remove-Item."

However, cmdlets can be combined to create custom scripts according to the logic of "pipelining," the function that allows the output of one command to be transformed into the input of another and that, in PowerShell, lets objects flow from one cmdlet to the next.

The result of pipelining (indicated by the "|" symbol) is a chain reaction that allows users to achieve more sophisticated solutions and process more complex data.

If you then think that PowerShell has over 200 cmdlets, it is evident how these chains can develop in size and customization, also relying on the use of conditional expressions, switches (used to replace a long succession of if/then/else conditions), loops and variables.

In addition, although cmdlets are rather self-explanatory, as we have seen, these come with their documentation, which you can access directly within the platform.

Or, you can use the "Get-Help" command, followed by "online," to retrieve Web articles that deal with the command or function you are looking for.

In this way, it will be easier for less experienced users to learn the different components of the system and how to use them for their automations.

But that is not all.

There are two other major aspects that we still need to explore to complete our overview.

The first concerns the possibility of using PowerShell remotely.

In fact, users can initiate commands and scripts from remote computers by leveraging the WS-Management protocol or Windows Management Instrumentation.

Without going into too much detail, just know that WS-Management is the protocol developed by the DMTF (Distributed Management Task Force), of which Microsoft is a part, to be able to manage and monitor the resources of a network regardless of the operating system being used.

Windows Management Instrumentation, on the other hand, provides users with a unified representation of a system's resources, thus enabling them to manipulate them remotely.

The second aspect to be emphasized concerns modularity, that is, the ability to organize cmdlets with related functionality into modules.

In addition, of course, to those created by the user, PowerShell provides 4 types that are already configured. Let's talk about the modules:

  1. Script.

Modules mainly used to import, export, and manage functions.

 

  1. Binaries.

The modules are manipulated to create cmdlets with more powerful functionality than can be obtained from the normal scripts offered by the platform.

  1. Manifesto.

Particular modules, which are characterized by being made up of text files, used to describe the components of a module.

Specifically, they define its cmdlets, functions, requirements and dependencies, thus helping users to develop and deploy different modules more easily and quickly.

  1. Dynamics.

Modules that are set up at the request of a script.

Because they fulfill a momentary need, they are generally not stored permanently within the platform.

In addition, you can download and install even more modules, which are also preconfigured but are not present in PowerShell.

These include those related to SharePoint site collections, their user groups, and security settings.

Since the topic of our next chapter concerns the relationship between PowerShell and a SharePoint Online tenant, the module we are interested in is precisely the one dedicated to the cloud version of SharePoint. Its name is "Microsoft.SharePoint.Online.Management.”

Sign up for our newsletter

You will receive:

  • Our guides to enterprise digitization;
  • In-depth articles on how to improve your intranet
    while making the most of your Microsoft 365 licenses.

You can unsubscribe at any time.

How to connect a SharePoint Online tenant
to PowerShell

Before we look together at the steps to follow to connect PowerShell to one of your SharePoint Online tenants, we need to do a little introduction.

PowerShell applies to any version of SharePoint, allowing users to manage platform settings at the organization and site collection level.

However, as anticipated, you need to download and install the specific module for the type of tenant you want to connect, depending on whether it is on-prem or cloud.

In the former case, the module you need is the one called "Microsoft.SharePoint.Management"; in the latter case, which is then the one we will explore in this chapter, the module you need to equip yourself with is called "Microsoft.SharePoint.Online.Management."

Although the cmdlets and features that the two modules contain distinguish them from each other, it is possible to find commonalities.

For example, there are always commands that specialize in creating a new collection of sites, defining their title, URL, owner, storage quota, and user groups with related permissions.

In addition, you can find cmdlets that allow you to get detailed reports and statistics regarding the usage and performance of the different sites that make up a collection.

Having said that, let us now turn to the core of our discussion and take the case that you want to use PowerShell to batch manage the operations behind one of your SharePoint Online tenants.

If you have not yet installed the relevant module, the fastest way to do this is to log in as admin to the PowerShell Gallery and run the "Install-Module-Microsoft.SharePoint.Online.Management" cmdlet.

Once that is done, you move on to the connection with the tenant.

The path branches in two parallel directions here, depending on whether your admin account has multi-factor authentication (MFA) or not.

Let's look at both cases.

 

1) Steps for admin account with multi-factor authentication

In short, here is how you need to proceed:

  1. Access the PowerShell Gallery.
  2. Run the "Connect-SPOService-URL(...)" command, specifying the address of your tenant.
  3. Enter your Microsoft credentials in the window that will open.
  4. Approve the login request, satisfying the authentication requirements.

And that's it.

Now that the tenant is connected to PowerShell, you can begin to serially manage and manipulate the activities that form its basis.

You will soon see that your IT team no longer has to waste hours or entire days configuring your company's sites.

2) Steps for admin account without multi-factor authentication

In this case, the process becomes significantly less demanding.

Here is what you need to do:

  1. Access the PowerShell Gallery.
  2. Run the "Connect-SPOService-URL(...)-Credential(...)" command, specifying your tenant's address and entering your UPN, which is the e-mail address that indicates both your username and domain.
  3. Enter your account password.

Once again, you are done.

Need help managing your SharePoint sites?


We have more than 80 hands to lend you to
:

  • Assign the right permissions to users.
  • Create the best processes to manage the life of your digital documents.
  • Optimize space and data storage arrangements.
  • Ensure the security of shared information.
  • Develop custom web parts and graphics for your business.

 

Giuseppe Marchi

 

Microsoft MVP for SharePoint and Microsoft 365 since 2010.

Giuseppe is the founder of intranet.ai and one of the leading experts in Italy in all matters related to Microsoft 365. For years, he has been assisting companies in creating their digital workplace environment on the Microsoft cloud, focusing on people's experience.

FAQs: Connecting to SharePoint Online with PowerShell

  1. How do I Connect to SharePoint online from PowerShell?

    • To connect to SharePoint Online from PowerShell, use the Connect-SPOService cmdlet. This establishes a connection to your SharePoint Online environment.
  2. Could not Connect to SharePoint Online using PowerShell?

    • If you encounter connectivity issues, ensure you have necessary permissions, correct credentials, and check internet connection and firewall settings.
  3. What is the Connect online PowerShell command?

    • The Connect-SPOService cmdlet is the PowerShell command for connecting to SharePoint Online. It requires specifying the SharePoint Online admin center URL and providing valid credentials.
  4. Can you use PowerShell for SharePoint online?

    • Yes, PowerShell is useful for managing SharePoint Online. It allows automation of tasks like site provisioning and user management, enhancing administration efficiency.

Keep on reading

SharePoint Sync: How to synchronize SharePoint libraries

Steps to follow, products to use, benefits and issues: find every detail about SharePoint synchronization here.

SharePoint App: How to use the intranet from mobile

Open, read, post, discover: This is your pocket SharePoint intranet.

Microsoft Loop: What it is and why it is going to change digital work

Loop is the new Microsoft 365 app, still in the experimental phase. What will it lead to?