While it is possible to manage your SharePoint Online using the Office 365 portal, there are some tasks which are much easier and quicker to do with PowerShell. If you want to optimize your SharePoint Online administration – PowerShell is your friend. Whenever you come across a repetitive administrative task, you can be sure that PowerShell will help you automate it, at least to some extent.
Before you perform any kind of an administrative task in your SharePoint Online, you need to connect to your organization. Mind that connecting to SharePoint Online might be useful not only when you want to manage your SharePoint sites and permissions, but also when you want to run some scripts on Office 365 groups. This article shows you how to take the first step to manage your online resources by connecting to your SharePoint organization with PowerShell.
How to connect to SharePoint Online
There are a few different approaches which allow you to get access to your resources in SharePoint Online. While all methods require you to use the Conect-SPOService cmdlet, they each offer a slightly different experience.
Method 1 – manual connection
The easiest method to connect your PS console to SharePoint Online is to run the following cmdlet:
Connect-SPOService -url https://*org-name*-admin.sharepoint.com
Mind to replace *org-name* with the actual name of your SharePoint organization. After you hit enter, you will see a familiar Office 365 login page:
Simply enter your credentials and the PowerShell console will connect to SharePoint Online. This method has one clear advantage – it works even when your account has MFA enabled. On the other hand, most people use PowerShell not to deal with the popups and additional windows and would rather avoid unnecessary popups. You will find this method especially troublesome if you decide to connect PowerShell to another module (like Exchange Online). Fortunately, there are different methods for accessing SharePoint with your console.
Method 2 – remember your credentials for the Session
If you do not wish to reenter your credentials each time you want to reconnect to your organization (or connect to another module), this method is optimal for you:
$creds = Get-Credential -UserName *yourUPN* -Message "Enter your Office 365 credentials";
Connect-SPOService -url https://*org-name*-admin.sharepoint.com -Credential $creds
The Get-Credential cmdlet will cause a standard PowerShell sign-in box to pop up. The -UserName attribute allows you to automatically fill in the login field. The credentials will be remembered as long as you keep your PowerShell console open and don’t overwrite the $creds variable. Mind that you can use your saved credentials to log in to other Office 365 services, for example:
Connect-AzureAD -Credential $creds
Connect-MSOLService -Credential $creds
Method 3 – connect without any sign in boxes
This final method allows you to login without any pop-ups or additional boxes – you perform each task in the console.
$login = *yourUPN*;
$pwd = *password*;
$pwd = ConvertTo-SecureString $pwd -AsPlainText -Force;
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $login,$pwd;
Connect-SPOService -url https://*org-name*-admin.sharepoint.com -Credential $credentials;
Some optimization-oriented admins are tempted to automatize the login procedure by saving their credentials in the script files. Unfortunately, this exposes login data in the script. It is not recommended, it is not safe, it is not good practice, but it allows automation at its finest.
Test your connection
No matter which method you choose, you will not get any “connection established” notification. Although no error usually means that you have connected successfully, you should make sure. To check if you have been successful connecting to SharePoint Online, you can run a simple cmdlet:
You should see the list of your SharePoint Online sites. If that’s the case, you should be able to perform any administrative task you have enough permissions to do.
Common problems and their solutions
There are a few problems you might run into while trying to connect with SharePoint Online.
Connect-SPOService : The remote server returned an error: (401) Unauthorized.
This error appears when the user does not have permissions to connect to the Office 365 organization via PowerShell. The remedy is simple – have your administrator run Set-User <your_identity> -RemotePowerShellEnabled $true
Get-SPOSite : No connection available. Use Connect-SPOService before running this CmdLet.
It seems that you have failed to successfully connect to your SharePoint Online organization. Go through the connection procedure once more and make sure you follow each step closely.
Connect-SPOService : The term ‘Connect-SPOService’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
You need to download the SharePoint Online module for PowerShell. Run:
Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | select Name,Version
to confirm if you have the required module (and also if you need an update), and use:
Install-Module -Name Microsoft.Online.SharePoint.PowerShell
To fix the problem.
Connect-SPOService : The sign-in name or password does not match one in the Microsoft account system.
There are two possible reasons for this error – either you have made a mistake while inserting your credentials, or you have a multifactor authentication enabled (MFA). If it is due to the MFA, you will need to remove the -Credential parameter while connecting to SharePoint Online and enter your credentials with the traditional, login page experience.
Files cannot be loaded because running scripts is disabled on this system. Provide a valid certificate with which to sign the files.
Run Get-ExecutionPolicy to see what is your Execution Policy. If it is restricted, use Set-ExecutionPolicy RemoteSigned to let yourself run the required scripts.
Accessing your SharePoint Online via PowerShell is an easy first step you have to take before you experience a whole new level of SharePoint management. Stay tuned for more detailed articles on how to manage SharePoint Online.