PowerShell 101 for SharePoint Friends

sharepoint-powershell

What I have found is that most people who still have not learned embraced PowerShell say they aren’t doing so because they think it is for the other guy. If you are an administrator, you say it is a developer tool. If you are a developer, you say it is an administrator tool. And if you are a site collection administrator you say meh. However, PowerShell is for EVERYONE!

 

 

The next section is all about convincing you why you should love PowerShell. If you don’t need convincing, then you can skip below to the section titled What Are the Basics You Should Know?

Why Should You Care About PowerShell?

Because I said so and everything you read on the internet is true. Here is the easy test to discover if you should learn PowerShell:

learn-powershell-2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I don’t think the flowchart can be any clearer — if you are going to continue to work with platforms in the Microsoft ecosystem, then PowerShell is an important part of your future.

PowerShell’s rise to dominance in the Microsoft world is because everything is easier and more flexible with PowerShell. Think about that new feature you want to see Microsoft add to SharePoint. There are two ways for the company to release the feature — as a new page in Central Administration or as a cmdlet in PowerShell. To make a new page in Central Admin takes a lot of work and testing. Central admin supports 50 different languages at last count, meaning that new page has to be written, translated, and tested in 50 different languages. Do you know how many languages PowerShell supports? One. So the next time you are annoyed that the task you want to do is only in PowerShell, now you know why. And this holds true for all of Microsoft’s products. That is why with every release you see more “cool” features that are found only in PowerShell. And because I know you like hanging out with the cool kids, you are going to have to embrace PowerShell to stay relevant.

Last point here because I want you to embrace PowerShell wholeheartedly. Go to your favorite job board and search for PowerShell or DevOps. That is where a lot of the technology jobs are headed, and while I am not saying you should head there, I am saying the more skills that you have that lend themselves to automation, the more employable you are. And because you didn’t select that you are going to win the lottery in the flowchart, a good job is a nice thing to have.

Where Can You Use PowerShell with SharePoint?

PowerShell works with all modern SharePoint versions, both on-premises and online. So, SharePoint 2010, 2013, and 2016 all work and come with the SharePoint Administrator Shell, which is PowerShell for SharePoint ready to go. Just make sure you always run it as an administrator.

SharePoint Online, and all of Office 365 for that matter, support PowerShell. You just have to manually install it on your system to get it up and running. To install it, you can either follow along on a boring Microsoft website or, because we are friends, I have made you a video that shows you how to install PowerShell for SharePoint Online and Office 365. I will let you pick which one you want to use to get things up and running.

What Can You Do with PowerShell?

Would it be hyperbole if I said everything? I don’t think so but just to be safe, I will provide more details.

This is a lot like the earlier paragraph about who is PowerShell for, in that it can do things for everyone. Although it seems that everyone knows you use PowerShell to install and configure SharePoint, a lot of people don’t think about the fact you can also use it to create, edit, configure, and delete content. The following are just a few examples of what you can do with PowerShell:

  • Create a list or 5,000 list items for testing
  • Change the look of every site in your farm
  • Add a view to every document library in a site collection
  • Get list of user permissions or usage across the farm
  • Run the client-side object model and pretend you are a developer

The possibilities are endless. It is very empowering when you get those bizarre, ridiculous requests from users and you realize that yes, you can write a PowerShell script to upload all of the documents from their desktop to their My Site and tag them with the original create and modify dates with metadata derived based on the color sweater they are wearing. Who needs a third-party tool? However, before you do anything that crazy, you need to learn the basics.

What Are the Basics You Should Know?

I have had the good fortune of helping thousands of people learn PowerShell over the past six years or so, and I always start in the same place. Learn these basic cmdlets and you will be all set. Learn these cmdlets and you can figure out the rest.

  1. Get-Command — You use this cmdlet to get a list of all of the cmdlets you have available to you, which is helpful as you understand that all cmdlets are in the form action-object (or verb-noun if that makes more sense). So, the first SharePoint cmdlet to start with is Get-SPSite, which will give you a list of all of the site collections in your farm by default. Fun. The good news is with that in hand, you know that SPSite is the object. So, if you run Get-Command -noun SPSite , you will see all of the cmdlets for interacting with a site collection. Another pattern that you will see emerge is that there is a set number of verbs. That way you only have a handful of verbs to learn, and they are consistently making learning faster.
  2. Get-Help — Now that you have found the cmdlet Get-SPSite, you will want to figure out all of the things you can do with it. The best way to do that is type Get-Help Get-SPSite . This cmdlet will give you a list of all of the parameters and usages of the cmdlet and a brief explanation. That is good, but PowerShell can do better. This time try Get-Help Get-SPSite -Examples . BOOM! Now you will get a handful of scenarios and how to get the results you are looking for with PowerShell. Some cmdlets even have online help that you can access by typing Get-Help Get-SPSite -Online . It is probably the same as -Examples, just a lot easier to read in a browser window.
  3. Get-Alias — Keep this cmdlet close while you are learning. When you start looking at articles on the internet, you are going to see a lot of odd things that don’t follow the rule of verb-noun, such as ?, %, CLS, and Select. Those are shortcuts that make PowerShell easier to use, but while you are learning shortcuts are bad. The good news is you can translate them with Get-Alias. For example, to discover what % is, you type in Get-Alias % That will tell you that the % is an alias for ForEach-Object. If that cmdlet is new to you, use Get-Help to find out what it can do. Another handy trick is if you just type Get-Alias by itself, it will show you a list of all of the crazy aliases that are built in.
  4. | — That weird symbol is called the pipe. You use it to send the objects from one cmdlet to another. That way if you wanted to get a list of all of the webs in your farm you could run Get-SPSite | Get-SPWeb . The Get-SPSite cmdlet would get all of the site collections objects in your farm and then send each one to the Get-SPWeb cmdlet, which accepts a site collection as input and then gives you a list of all the webs in that site collection. And don’t forget that you can have multiple pipes. Some of my favorite scripts are piles, and piles of cmdlets strung together with pipes.
  5. Get-Member — This one is a little more complicated, but it is the gateway to you changing the world. This cmdlet shows you what methods and properties are available for an object. Methods are the things you can do to an object and properties are the characteristics of the object. For example, type in Get-SPSite | Get-Member . This cmdlet will show you all of the things you can do to the site collection object (methods) and the info about the object (properties). So now you know there are methods for deleting and upgrading the site collection. And there are properties such as Quota and Owner that might be helpful to query or update. We will cover how to manipulate the object in a later article, but this shows you the true depth of PowerShell. You can do some scary things.
  6. Start-Transcript — I wish someone had shown me this on day one. Start-Transcript  writes everything that you enter into the console session and the output to a text file. I recommend that you run this cmdlet first every time you open PowerShell. That way, you have a record of everything you have done. When I was first learning PowerShell, I would spend an hour learning something new, only to close PowerShell. Then the next day I would have to start over. Don’t be me; it was no fun learning looping three days in a row because I didn’t save my work. Transcripts are a must.

Wow, are you exhausted? I hope so; that means you are learning. The good news is now that I have made you read and learn that way, I am going to reinforce it with video. I am such a good friend. You can either learn with the video PowerShell Intro without SharePoint context, which makes it easy to do from your local PC or you can watch Get Started with PowerShell for SharePoint On-Prem to learn in a more comfortable environment. In a later article, we will take a deep dive into getting started with PowerShell for Office 365, but there is a video if you aren’t the patient type. Be warned, though, that video doesn’t teach you the basics like the other two. You will need to watch it after you have learned more about the core cmdlets.

Hopefully, this article has gotten you excited to learn more about PowerShell. In the next article, we will explore how to manipulate objects in PowerShell, which means we will write one of those fancy scripts and make it all make sense. Something roughly like the following, plus any twists I decide to add while I am writing:

Get-SPDatabase | %{$db=0} {$db +=$_.disksizerequired; $_.name + " - " + $_.disksizerequired/1mb} {Write-Host "`nTotal Storage (in MB) =" ("{0:n0}" -f ($db/1024/1024)) -backgroundcolor magenta }

After that, we will look at fun things such as how to create and edit SharePoint lists and their items. I hope you are looking forward to it because I know I am! In the meantime, if you need any help, you can always hit me up on Twitter @ShanesCows or check out the PowerShell resources on the Petri IT Knowledgebase. With all of that knowledge, you can get real fancy.