The purpose of this post is to show the quick steps to connect to O365 Services with PowerShell and in particular with the SharePoint Online Services.
Before proceeding here you are the shopping list.
- A valid O365 account with Global Admin rights
- A Windows OS (preferably 64-Bit edition)
- Microsoft .NET Framework 4.5.x
- Windows Management Framework 3.0 or 4.0
- Microsoft Online Service Sign-in Assistant for IT Professionals RTW
- Windows Azure Active Directory Module for Windows PowerShell (64-bit version)
- SharePoint Online Management Shell
At this point we are ready to start by executing the following steps:
Step 1: Open Windows PowerShell as an administrator
For convenience I prefer to use the PowerShell ISE. When starting with the Administrator account will look like the following:
At this point we need to import the MSOnline and the Microsoft.Online.SharePoint.PowerShell modules with a classic Import-Module. We can then verify the modules are loaded successfully with a simple Get-Module. Please note that in order to avoid a warning message when loading the module for SharePoint Online we’ll use the -DisableNameChecking parameter as per screenshots below:
And we can now proceed with the module to manage SharePoint Online. If we forget to add the DisableNameChecking parameter we’ll get the following warning message: The specified module ‘Microsoft.Online.SharePoint.Online.PowerShell’ was not loaded because no valid module file was found in any directory
Now with a simple Get-Module we can verify the needed modules have been loaded correctly
Step 2: Create a Windows PowerShell credentials object
At this point we’re ready to create the credentials object. We can do this by saving them in an encrypted format into a variable by using the credential window as per next screenshots. Let’s create a variable $Credsecurely storing our credentials:
When prompted enter the credentials for an o365 user with Global-Admin role. Should you have a custom Domain associated with your o365 Tenancy, enter directly the required credentials using your custom domain rather than email@example.com
To verify the credentials are successfully stored we issue the command for the associated variable:
Step 3: Connect to Office 365
At this point we are ready to connect to the o365 tenant by issuing the command:
Connect-Msolservice -Credential $Cred
After connection is established there is a big number of commands we can execute to query and execute actions on the o365 objects. From here we can also manage Users and Groups, Contacts, Directory Synchronization, Domain Info and much more.
Step 4: Connect to SharePoint Online
Now we are ready for the last step: connect to SharePoint Online. For this scope we’ll use the Connect-SPOService command making sure we remember the following simple rules
Always use the HTTPS in the URL paramter
Build the URL with TenantName-admin.sharepoint.com
After the connection is successful we can then use the Get-SPOSite to list the available Sites
There are plenty of commands already available for managing SharePoint Online and these will be part of my next posts.