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.
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.
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.
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:
- 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:
Modules mainly used to import, export, and manage functions.
The modules are manipulated to create cmdlets with more powerful functionality than can be obtained from the normal scripts offered by the platform.
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.
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.”
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.
In short, here is how you need to proceed:
- Access the PowerShell Gallery.
- Run the "Connect-SPOService-URL(...)" command, specifying the address of your tenant.
- Enter your Microsoft credentials in the window that will open.
- 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.
In this case, the process becomes significantly less demanding.
Here is what you need to do:
- Access the PowerShell Gallery.
- 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.
- Enter your account password.
Once again, you are done.
Keep on reading
Steps to follow, products to use, benefits and issues: find every detail about SharePoint synchronization here.
Open, read, post, discover: This is your pocket SharePoint intranet.