From the course: PowerShell for Microsoft 365 Administration

Connect to SharePoint Online and OneDrive for Business

From the course: PowerShell for Microsoft 365 Administration

Connect to SharePoint Online and OneDrive for Business

- In order for us to connect to SharePoint Online, and OneDrive for business, utilizing PowerShell we first need to import a specific module, or at least install the module and then import it into the current session, so we can actually make a connection out. The first thing we have to do though, is check whether we actually have the module installed, and import it into our current session. So, I can say Get-Module and you'll see this will return the list of modules that I have available to me in the current session. So, clearly there's nothing that says SharePoint in the name, so that means the module isn't available to me. To add the module, we first need to say Install-Module, we can pass a name to it at this point which will be Microsoft.Online.SharePoint.PowerShell. Now, not every module is named as long as this one, but the SharePoint one is a little bit longer than the rest. So, I'm going to say install module, this will then prompt me because it's going to try and connect to the PowerShell gallery, and then I can choose the yes option to install. And what you should see is the blue bar suddenly flicks up, tells me it's installing, and then it should just install it and then complete. Now, if we're looking at other types of PowerShell modules, some of these can take a while to install and download because of the size of the files. Okay, so now we have installed the module. Let me go back to my Get-Module command and enter, you'll notice I don't get the SharePoint one, but I do get things like PowerShellGet, and I get some management and utility ones and package management, so some of these were there before, but now we've added to it because we ran the installer option. Now, the first thing we have to do to utilize it is import that module directly into the session that we're in. So, that's one of the keys about PowerShell is you have to include the modules in the session that you'll utilizing, otherwise the commands are not available, so let me just do this. So, import the module, and of course it'll come back and complain and sometimes it does, and it will complain about that being unapproved verbs. Now, if you remember how PowerShell works, it utilizes a verb noun scenario, so things like add, get, new, remove, et cetera. There will be some commands that don't follow that model. So, now that we have that I can say, Get-Module, and sure enough we should see that top one, Microsoft.Online.SharePoint.PowerShell, and then you'll see the exported commands. Now, what I can do here is I can say, well, I want to get a module I could say by name. So, let's do Microsoft.Online.SharePoint.PowerShell, I could do this and it will return me the same information. I could then do this and just say Select, expand the property which is called ExportedCommands, and just do enter. And then sure enough what you'll see now is a list of all of the commands that are available to me now that the module is installed. So, fairly straightforward so far. Now, of course, once we have it installed, what we then need to do is to look at how to connect to SharePoint Online and OneDrive, and that's done by utilizing a couple of different approaches. The first one is to actually utilize a credential object which is what we'll do, so we'll create a variable called user. And what we'll do is put our office 365 user account directly in as the account we wish to utilize, so I'm going to paste that into here. You'll notice two things about this. The first thing is this is the admin account that I'm using, and you'll see it's got the prefix for the domain. So, I'm going to create another variable here called tenant, and what I'll do is I'll just select this option and then I'll paste that into there, so that gives me the prefix of the domain for the tenant because when we utilize SharePoint Online, predominantly if we're doing administration functions, we connect him to the admin URL. Now, the admin URL of a SharePoint admin center is normally the tenant-admin.SharePoint.com. Whereas site collections will be tenant.SharePoint.com/ and then whatever the path might be afterwards. So, we have a user and then we have a tenant that we wish to utilize. Now, the command to utilize for this is called Get-SPOService, and the Connect-SPOService is the command that we utilize by passing in these various options. So, the first thing I'm going to do here is I need to create some kind of connection, so I'm going to get rid of connect here. The first thing I want to do is I wannna create a credential object because the connect one supports not only the standard passing in credentials, but also the browser as well. So, let's start with the first one, so I'm going to say, Get-Credential, I'll say UserName, and then I'll pass my user, and then there's also a message box, and I'll just say Message. Normally you would say something like, please type your username and password. So, I'm going to press enter and you'll see it pre-populates my account. I'm actually just going to copy my password here, and then paste it in and click okay. Now, what did that create? Well, if I click creds, you'll see it created a username and a SecureString, so a key value pair which is my credentials. Once I have that now I can then utilize that as part of that other command for connecting. So, if I go here and say, Connect-SPOService, you can see, I need to pass in a URL, so remember, we're going to use https://$tenant, which is my variable -admin.sharepoint.com. What I can then do is choose the credential object and say creds and press enter. Now, you'll notice it doesn't return any values back, so how do you know whether it's connected? Well, the easiest way is to just utilize an existing command, like Get-SPOSite, press enter and sure enough if it returns values from this list of site collections, then of course you are connected. Now, we can also disconnect as well a specific session, so I can say Disconnect-SPOService if I just clear that, and then I go back to Get-SPOSite, you'll see that that's not going to work because I have no valid connection. So, what happens if you're trying to connect and you have to go through either a third party authentication mechanism from Azure AD, or maybe multi-factor authentication 'cause that creds object doesn't function that way. Now, what we can do here is we can go back to our list, get rid of the credential options and just simply pass in the URL. Now, if I click enter here, now what's going to happen is it will launch a browser window that I can then enter credentials into. There you can see that we've got that browser window available to me, so let me just copy my credentials, I'm going to paste these into here, so same thing again, we have the password and click sign in, same thing again. How do we know if it's functioned, or let's do Get-SPOSite and sure enough it has to. Now, of course it was the same process for me because I wasn't utilizing multi-factor authentication for that, but if I did the browser one would have supported that, whereas the other one would not. So, that's how we install the module, import the module, and then make a connection to SharePoint Online and OneDrive for business.

Contents