Connect to Office 365 and SharePoint Online using PowerShell

SharePoint Online Permissions Management

Connect to Office 365 and SharePoint Online using PowerShell

In this article, we will create new users to your SharePoint online environment, and also add these users to Office 365 Groups using Remote PowerShell. This tutorial should help get you set up and started with PowerShell for SharePoint Online.

PowerShell scripting has been the most efficient way to automate the tasks as it is just the command shell language but also the scripting language. With PowerShell we can execute the tasks more efficiently than if using the GUI. For example, repetitive tasks and ‘templates’ can be used to help automate your workload. It may seem difficult at first, but after a few goes, you will not turn back:) Over the last few years PowerShell has been a very useful tool and Microsoft has been investing a lot in it as it is a part of Microsoft engineering criteria, so be sure that there is a lot of support for this method of SharePoint Online user and permissions management.

Pre-requisites

Before working with Office 365 PowerShell commands, we will have to download and install Microsoft Online Sign-In Assistance for IT Professional and the Windows Azure Active Directory Module for Windows PowerShell.

Install Microsoft Online Sign-In Assistance for IT Professional

You can download the Microsoft Online Sign-In Assistance for IT Professional from here. Run the executable as below.

Install Microsoft Online Sign-In Assistance for IT Professional 1

Click on finish.

Install Windows Azure Active Directory Module

Download the Windows Azure Active Directory Module from here.

Run the downloaded installer file and click on next.

Install Windows Azure Active DirectoryInstall Windows Azure Active DirectoryInstall Windows Azure Active DirectoryInstall Windows Azure Active Directory

Once the installation is completed, click on finish.

 

Launch the Microsoft Azure Active Directory PowerShell

Open the Microsoft Azure active directory powershell and execute the below command

Connect-MsolService

This will launch the login screen for your azure active directory PowerShell.

Input your office 365 admin username and password and it should successfully authenticate your credentials.

In order to verify that you have been connected to your office 365 tenant using azure active directory, execute the below command

Get-MsolUser

It should display list of all the users in your office 365 tenant.

Creating the User using remote powershell

Now let’s say if we want to create the new user in the Office 365. We will at least require Display name and User principal name. Other parameters that are good to have are FirstName, LastName, LicenseAssignment and Password

LicenseAssignment is the licensing plan available for your office 365 tenant. Now to get the value of the LicenseAssignment available in the office 365 tenant execute the below command in PowerShell.

Get-MsolAccountSku

This command will return the licensing plan available in your office 365 tenant and will also show how many licenses are already consumed and how many are active. We will assign AccountSkuId to the LicenseAssignment.

For Password we can create a random password for the user but if we don’t specify the password it will create the password automatically for you and display it in the output of the command.

For creating the user in Office 365 using PowerShell execute the below command

New-MsolUser -DisplayName <DisplayName> -FirstName <FirstName> -LastName <LastName> -UserPrincipalName <Account>  -LicenseAssignment <AccountSkuID> [-Password <Password>]

Create Office 365 Group using the PowerShell

Now to add the user to the office 365 group, let’s first create the office 365 group.

Execute the below command to create the group in office 365

New-MsolGroup –DisplayName “TestPowerShellGroup”

This will create the PowerShell group with the name “TestPowerShellGroup”

In the output it will display the object id of the group, please make a note of it.

Create New O365 Group Powershell

Add users to Office 365 Group using PowerShell

Now to add the user to the office 365 group that we just created we will use the below command

Add-MsolGroupMember -GroupObjectId <Object Id of the Office 365 Group> -GroupMemberType User -GroupMemberObjectId <Object Id of the User>

This command require GroupObjectId and GroupMemberObjectId as the parameter value. We already have the object id of the group created in our previous step. To get the object id of the User, we will execute the below command

Let’s say we want to get the object id of the user with User principal name as test@qipoint.com.

We will execute the below command,

Get-MsolUser -UserPrincipalName “test@qipoint.com” | Select ObjectId

This will output the ObjectId of the user and we will use this in the command to Add user to the group.

Example,

Add-MsolGroupMember -GroupObjectId 53877c2f-c864-4aaf-847f-5da19e364aa3 -GroupMemberType User -GroupMemberObjectId bbb55777-d5aa-499d-abbf-353d4523049f

 

I hope this article was useful. Please let us know your feedback or queries in the comment section below. Thank You !

Do you use SharePoint? Try our toolkit
Download SharePoint Essentials Toolkit Now
Download the SharePoint Essentials Toolkit
Dhaval Shah - MCD, MCSD
Follow me
Latest posts by Dhaval Shah - MCD, MCSD (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.