Getting Started with PowerShell for SharePoint Online and Office 365
Ah, the cloud. The promise of no more managing servers and the magic of automation everything. If you have moved to Office 365 you have at least gotten the first step done. No more servers for you to hug in the cold, smelly, and noisy server room. And if you think your server room smells you should have been at my first job, we had the halon go off one time. Not cool. Anyway.
Now that you have achieved the no server hugging lifestyle, it is time for you to move on to the automation part. To help you get more of that gift in this article, we will dive into PowerShell for SharePoint Online and Office 365. We will talk about the different cmdlets available, how to get them installed, and then the tricky part of connecting.
If all of this PowerShell talk has you freaked out, don’t fret. You can check out my earlier articles on Learning PowerShell and Working with PowerShell Objects and Scripts. They will excite and fascinate you as you learn all about the basics of PowerShell that you can build on to become a guru.
What Do You Need to Install?
Because Office 365 isn’t built into Windows (yet?), you are required to install the necessary modules and helpers to connect to and use PowerShell. This will include modules for SharePoint Online, Skype for Business Online, and Azure Active Directory (AD) for Office 365. We will also talk about Exchange Online functionality, but you don’t have to install those cmdlets; instead you will use PowerShell remoting.
Another option for us “SharePoint people” is installing the Patterns and Practices (PNP) PowerShell module from Microsoft’s PNP team. These cmdlets are pretty awesome and allow for you to do more, a lot more. They have cmdlets for things such as creating and working with lists and their contents, getting and setting all kinds of properties, and more. Right now, there are 181 cmdlets and they are all written with a purpose.
Although I would like just to jump straight to the PNP PowerShell, we are going to save that for another article. Not because I am mean, but because you need to get started with the default cmdlets so that you know what functionality they offer and so that you can appreciate the magic of PNP. Also, while PNP is great for SharePoint, it doesn’t help you manage accounts, licenses, Exchange, Skype, or the rest of your Office 365 experience. That is what the default cmdlets are made to do. So, let’s get up and running with them today.
Installing the Modules
One of the many miracles of PowerShell is how easy things can be to install. A couple of fast lines and boom you are up and running, but before you can do that, you will need to install the sign-in assistant. All very quick and very easy.
- Install the Sign-in Assistant. You can download it from here. You will have to choose between 32bit and 64bit. You want 64bit because the 32bit stuff has been deprecated. Once you download it then you will run it, accept the license terms, click install, and click finish. Pretty darn easy.
- Install the Azure Active Directory Module. Yes, you need it. Remember Office 365 is built on top of Azure AD, so you are using it even if you don’t think that you are. You can download it from here. You can choose between the general availability (GA) bits or the preview bits. If you are still in the learning phase, pick GA. Once you download it then you will run it, then click next, accept, next, next, install, and finish. No sweat worked up here.
- Install the SharePoint Online Module. Probably the piece you care about most. You can download it here. Pick the 64-bit version. Once you download it, then you will run it, accept the license terms, click install, and finish. Still easy.
- Install the Skype for Business Online Module. I will skip the Skype name jokes for now and just soldier on the path. You can download it from here. Once you download it, then you will run it, accept the license terms, click install, and close. Why couldn’t they also use Finish like the other installs?
Now that you have everything installed, it is time to figure out what happened to Exchange Online and to get connected for the first time.
Before You Connect, a Few Things to Consider
You need to run PowerShell as an administrator. Just right-click the PowerShell icon and select Run as Administrator. You should be pretty used to this requirement, since pretty much everything in PowerShell requires this privilege.
You need to be able to run RemoteSigned scripts. PowerShell security varies based on what version of PowerShell you are running and on what operating systems, so double check your settings really quick. In your PowerShell window, type Get-ExecutionPolicy . If you get Restricted, then you will need to change that; if you get any other value, you should be good to go. If you need to make a change, I recommend that you use the Set-ExecutionPolicy RemoteSigned command, but because we are now talking about changing your PowerShell security settings, I would also encourage you to understand the settings better. The good news is that RemoteSigned is the most restrictive setting that will allow you to use PowerShell with Office 365.
You need to be an Office 365 administrator. I don’t make up the rules, so don’t yell at me. The good news is that if you are not an Office 365 administrator, you can still use the PNP cmdlets. Guess you will have to wait until the next article. Sorry.
I assume you are running PowerShell 3.0 or later. I cannot imagine why you wouldn’t be, but I wanted to call this out. The main reason is that with 3.0 and later PowerShell automatically imports modules as you use their cmdlets. So I am skipping all of the import module steps. If you want to check your PowerShell version, you can use $PSVersionTable.PSVersion and you will get all of the info you need. You are just looking for a 3 or greater in the Major column. I am on version 5 with my Windows 10 desktop. Typically, I do all of the following steps on my local desktop though if you wanted to do it on a server, there is no reason you could not do so.
Connecting to SharePoint Online and the Rest of Office 365
The easy part is done, now time to help you get logged in. Make sure you followed the previous section and opened up PowerShell after you installed all of the new modules. If not, things will not work the way you need them to.
Store your credentials. Office 365 usernames and passwords are complex and long, so it is best if you only type them once. To do this, we will save them to a variable by typing in $MyAccount = Get-Credential . This will open a pop-up box in which you can type in your username and password and store them to the variable you just created. Keep in mind this is not validating your account right now, so if you fat-finger your password you will not find out until a later step when you get an authentication error.
Connecting to the Office 365 service. That is a simple line of PowerShell Connect-MsolService -Credential $MyAccount . This line will enable you to use the account specific cmdlets for managing users and license among other things. If you want to confirm that you are logged in successfully, then try out Get-MsolUser ; if you see a list of all of your Office 365 accounts, then you are done here.
Connecting to SharePoint Online. The one liner here is Connect-SPOService -URL https://MyTennatUrl-admin.sharepoint.com -Credential $MyAccount . You just need to update the URL option with your value. So if your SharePoint Online URL is https://contoso.sharepoint.com, then for this cmdlet you would use https://contoso-admin.sharepoint.com. See what I did there? You just need to add in the -admin part, and you will be in business. And to make double sure you are in, you can type Get-SPOSite and you will see a list of all of your site collections. Hooray.
SharePoint bonus content because I love you: If you want to see all of the cmdlets that you now can use with your SharePoint Online subscription, type in Get-Command -module Microsoft.Online.Sharepoint.PowerShell . I expect something nice on Valentine’s Day now.
Connecting to Skype for Business. Step one here is to remember that Skype, Lync, and Communication Server(CS) are all the same thing. These cmdlets and the documentation seem to rotate through the names, so don’t let it confuse you. Also, for Skype you don’t run the PowerShell locally you are going to remote to your online server. To do so, first run $Skype = New-CsOnlineSession -Credential $MyAccount then Import-PSSession $Skype . If you get any messages about starting WinRM, you should respond with Y to allow it to start. That will get you all hooked up and able to run the cmdlets you need. You can try Get-CsOnlineUser to see all of your Skype users and way too much information about them.
Important mental note: You created a remote session. When you are done, you should not just exit out of PowerShell; you need to kill your session first by typing Remove-PSSession $Skype. You are limited in the number of sessions you can have open at a time. All of this also applies to Exchange Online.
Connecting to Exchange Online. Last one. This is the same as Skype in that you are going to remote into the server instead of running the cmdlets locally. To get started, you need to do $Exchange = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri “https://outlook.office365.com/powershell-liveid/” -Credential $MyAccount -Authentication “Basic” -AllowRedirection . Just cut and paste instead of trying to figure out why this line is so complicated. And once you have run that line then you can type Import-PSSession $Exchange to start using PowerShell. To confirm it all works, try out Get-Mailbox to see a list of mailboxes. Pretty cool stuff. You will also find that the Exchange team provides a lot of cmdlets for your PowerShelling pleasure. The last reminder is you need to Remove-PSSession $Exchange when you are done. This frees up the session for you or the next guy to use.
Putting a Bow on It
That is it, folks. You now have all of the pieces of the puzzle that you need and hopefully enough context to make you productive (and dangerous?). The good news is that because I have declared my love for you, I have also made a companion video for this article; check out Install and Configure PowerShell for Office 365. So you can watch me go through all of these steps and add more color along the way.
The next article we will walk through the fun that is the Patterns and Practices PowerShell for SharePoint Online.
More in Uncategorized
M365 Changelog: Plus addressing: New disable setting and on by default
Mar 16, 2022 | Petri Staff
M365 Changelog: Reminder: IE11 desktop application retires in 3 months on June 15, 2022 – Set up IE mode today (non-LTSC, non-Server)
Mar 15, 2022 | Petri Staff
M365 Changelog: Introducing monthly active users for the Microsoft 365 service change
Feb 24, 2022 | Petri Staff
M365 Changelog: Microsoft OneDrive & SharePoint Library dropdown menu
Feb 3, 2022 | Petri Staff
M365 Changelog: People centric search rollout update (SharePoint, Office.com)
Feb 1, 2022 | Petri Staff
Microsoft Teams Rolls Out CART Captioning Support For Meetings To GCC Customers
Dec 17, 2021 | Rabia Noureen
Most popular on petri