close

Breaking Down The Basics of PowerShell Commands For SharePoint 

Posted by Errin O'Connor on Aug, 30, 2022 03:08

PowerShell is one of the most popular ways to script SharePoint. It’s a scripting language that lets you automate tasks, create scripts, and interact with SharePoint on your terms. SharePoint administrators and developers use PowerShell to automate workflows, simplify configuration tasks, and manage SharePoint sites.

SharePoint administrators have long been frustrated by the complexity of configuring solutions across multiple sites in a SharePoint environment. Think of a typical scenario where a development team builds a new web component that has to be introduced to all currently existing webs in an environment to give you a specific example. A developer might write a simple PowerShell script that uses the Central Administration site template to deploy the web part onto every site on the farm.

But this is only half of the story—the other half automates the deployment process. This is where SharePoint comes in handy again: it has built-in tools that make it easy for you to deploy applications without needing any programming knowledge (although it helps). Moreover, because administrators were no longer required to understand, this new method of utilizing PowerShell to interface with SharePoint made scripts more accessible and readable.

How To Work With External .NET Assemblies Using PowerShell for SharePoint

We believe that the ability for PowerShell scripts to leverage external.NET assemblies is what elevates the scripting tool to the next level. For example, imagine that your company’s engineers have created a.NET utility that exposes several methods to be utilized by different.NET services and apps.

PowerShell may use the functionality inside these tools for scripting if the methods in the generated assemblies are publically available, allowing any external process to use them.

Logic loops, variables, methods, and other programming constructs may all be found in PowerShell scripts. Typically, a PowerShell script is saved as a plain-text file with the.ps1 suffix. Except for its blue backdrop and white lettering, the PowerShell console resembles the traditional command-line console in every way. 

sshot

Customize Your PowerShell Environment With Profiles

You may have noticed that the default PowerShell get-command output looks much like what you’d see on the command line. PowerShell is built on top of .NET and supports most of the same features. For instance, dir or dir -recurse can be used to list every file in a folder or every folder in a directory, respectively. You can also use copy to copy a file from one location to another and paste (Ctrl+V) to paste the text into another application window.

PowerShell profiles are scripts executed whenever a computer starts or users log into their environments. Profiles allow users to customize their settings by default and instantiate global objects and variables, so they’re available everywhere in their session. 

Think of them as “startup scripts” that allow you to personalize your environment and make it feel more familiar to you. A typical example is setting your Path environment variable so that any commands you run now start with your current directory instead of searching for files based on its name.

Using PowerShell You Can Automate And Manage Microsoft SharePoint Online

SharePoint one of its most valuable features is the ability to create and manage SharePoint Online sites. You can use these sites to share information with others, or you can use them to store personal files. In addition, you may use PowerShell, a scripting language, to automate SharePoint-related processes in your scripts. Using PowerShell, You Can Automate And Manage Microsoft SharePoint Online will show you how you can do this easily.

SharePoint Online has much power, but it can also be cumbersome if you don’t know how to automate tasks. You can use the techniques in this article to create a script that will provide you with the URLs for all subsites on your SharePoint site collection and a quick visual indicator to spot child sites without parental approvals.

This process is helpful when a large amount of information is shown on the screen. Consider a PowerShell script that loops over every subsite in a SharePoint site collection with more than 1,000 sites, for instance. Let’s say the script’s goal is to locate subsites with specific permissions, but you also need to get a comprehensive list of all sites. The URLs of all sites that inherit permissions from the parent would be displayed with green background in your script, while you would display the URLs of all subsites with individual rights with a red background.

how to run a powershell script for sharepoint online

Manage PowerShell Commands For SharePoint 

SharePoint has several built-in features that allow you to manage your SharePoint environment. The SharePoint Farm environment that we will have created and configured by the end of this chapter will act as the foundation for all other chapters to come. All necessary components will be put in a single box, making the environment “self-contained.” In real-world circumstances, you should always host your SQL Server installation on a different server and offload the domain controller function to a different server.

Assigning preset roles to our servers, ensuring that the right services are running on the correct server, provides SharePoint more flexibility than its forerunners. For example, to make the SharePoint Web front-end servers as small as possible, components like the workflow engine, machine translation, and search services would often be placed on external application servers.

Why You Should Run PowerShell Scripts From SharePoint Sites

We will talk about the different levels of SharePoint artifacts, including site collections, sites, lists, items, and the like. Each will be examined in further depth and learn how they relate to PowerShell.

We will start by discussing the object model that allows you to write PowerShell scripts to interact with SharePoint artifacts. This process is necessary because you may have some experience with classic .NET development and therefore have some familiarity with concepts like ADO.NET and LINQ syntaxes that can help you write your script.

You will be familiar with the various SharePoint components and be aware of the attributes and methods made available by the object model. As was said before in this book, we presume that at this point, you have been exposed to SharePoint to some extent and are familiar with its operation.

sitedesignsitescriptpowershell

Retrieving a Single Site Collection Object from PowerShell for SharePoint

SharePoint is a server platform, and PowerShell is a command-line tool that you can use to interact with SharePoint by calling methods on the objects exposed by SharePoint.

In the context of SharePoint, PowerShell is all about interacting with the various artifacts made available by the server platform. SharePoint objects expose properties and methods just like any.NET object. For example, a list item exposes a property that indicates its last modified date. It also exposes a method that can induce a checkout operation on it. 

All these properties and methods are available in PowerShell, and you can make direct calls to them as long as you obtain a reference to the object you wish to modify using a retriever method. We refer to methods as retrievers when their sole purpose is to return a single or an array of objects. For example, the Get-SPSite cmdlet that returns a site collection object is a retriever method.

Using PowerShell To Create Team Workspaces In SharePoint

We learned how easy it is to interact with SharePoint webs using PowerShell. You will now put your new knowledge to good use and continue to build on the example started in the previous section of this chapter. Now that you’ve created a site collection for each business unit within your organization, you will create workspaces for each team within the business unit.

You will declare a multidimensional array with two dimensions: one representing the unit and one that will contain its associated teams. The logic will loop through each row and create the new webs using the Team Site template. To make things more exciting, each business unit will see their webs use a different theme to ensure you can easily differentiate them.

You’ve reached the last step in your journey: setting up a team workspace in SharePoint. Your next step is to define what the theme will be for each of these team sites. In SharePoint, themes are now referred to as composing looks. However, the ApplyTheme method is what you need to use to apply a custom theme. A composed look comprises three essential parts: a color palette, a font scheme, and a background image. You will declare a second multidimensional array to keep track of which you will apply a composed look to which web.

You Need To Know About EPC Group’s SharePoint Support

Hopefully, this article has gotten you thinking about writing your scripts and getting to know PowerShell better. It’s not hard and can help streamline your processes and enhance the SharePoint experience for everyone involved. The takeaway from this is that SharePoint is a powerful tool that provides the user with flexibility but lacks the power to automate tasks. The solution, of course, is PowerShell. However, even if you don’t need any help automating your tasks, it’s still helpful to learn about PowerShell.

Managing a SharePoint environment is never easy, but PowerShell has been designed to make some tasks easier and faster. While most administrators and generalists may not know the full extent of its power, many SharePoint-specific commands can make a tremendous difference in how effectively you manage your system. With help from EPC Group and Microsoft’s vast library of resources, we hope this article has helped open the door to PowerShell’s vast possibilities in SharePoint administration.

About the Author

Errin O'Connor

With over 25 years of experience in Information Technology and Management Consulting, Errin O’Connor has led hundreds of large-scale enterprise implementations from Business Intelligence, Power BI, Office 365, SharePoint, Exchange, IT Security, Azure and Hybrid Cloud efforts for over 165 Fortune 500 companies.

Let's Get to Work Together!

Talk to our Microsoft Gold Certified Consultants

Contact EPC Group

Call for help:

(888) 381-9725

Email Us:

[email protected]

Head Office:

4900 Woodway Drive - Suite 830 Houston, Texas 77056