Office 365: Security Groups Management using Powershell

Security Groups are the crucial part of any system as they define the Authorization on the available resources for the Users requesting access. They are also important to categorize permission boundaries for the set of users at once. So it is really important to streamline the process of managing Security Groups for any system.

Though the management activities are repetitive and boring if we need to repeat same steps again and again over the period of time.

Thankfully we can automate these repetitive tasks using PowerShell Scripts which can take inputs from the CSV or Text Files for the input values and perform necessary actions.

In this article we will discuss the automation scripts required for managing Security Groups in Office 365 using PowerShell.

If you want to follow along then the prerequisite for this article are:

  • Having a O365 Account created
  • PowerShell for Office 365 configured

If the above prerequisites does not meet then I would recommend you to read one of my earlier article “Office 365: How to Configure PowerShell for O365” to get to know the steps of installing prerequisites.

Lets’ login to O365 Account and visit the Admin Center

1

Navigate to Admin Center by clicking the “Admin” Tile on the Application Dashboard as shown below

2

Now in the upcoming sections we will see to the respective PowerShell Commands to deal with each of the management tasks

How to Add new Security Groups

Navigate Admin Center => Groups

For the first time in my case there are no security groups are present since this is a new O365 Account

3

Run “New-MsolGroup” command where “DisplayName” specify the name of the Security Group and “Description” specify the description for the group as shown below

New-MsolGroup -DisplayName “Test Security Group” -Description “This is created for testing.”

4

Once the command gets executed successfully, navigate Admin Center  => Groups to verify that new group has been added.

It is worth to note that “Default Group Type” for any group added using the above command will be “Security”

5

 How to export all Security Groups

We can export all the Security Groups from the O365 account by using “Get-MsolGroup” command as shown below

Get-MsolGroup

6

Once the command is executed successfully we can see the details of all the available Security Groups on the Host Window. Alternatively we can export the results to text files by piping the results to “Out-File” command.

How to export all Security Groups filtered by Group Properties

We can export a filtered set of Security Groups from the O365 account based on any property of the Group. In the following example I am using “DisplayName” property to filter the results as shown below

Get-MsolGroup | Where-Object {$_.DisplayName -eq “Test Security Group”}

7

Once the command is executed successfully we can see the details of the specific Security Groups matching the filter criteria

How to export all Security Groups filtered by Group Type

We can export a Security Groups based on its type also by using “GroupType”. In the following example we are going to filter all Security Groups which are of type “Security” from the O365 account.

Get-MsolGroup -GroupType “Security” | Where-Object {$_.DisplayName -eq “Test Security Group”}

8

Once the command is executed successfully we can see the details of all the Security Group of type “Security” and with DisplayName = “Test Security Group”

 How to Add Users to Security Groups

Run the following command to check the existing members which are present in the Security Group

$securityGroup = Get-MsolGroup -GroupType “Security” | Where-Object {$_.DisplayName -eq “Test Security Group”}

Get-MsolGroupMember -GroupObjectId $securityGroup.ObjectId

9

Once the command executed successfully we will get the list of Users already added to the group

We can see the same information by Editing the Group with in the Browser as shown below:

10

11

We can add new members to the Security Group by using the following command

Create Object required Security Group

$securityGroup = Get-MsolGroup -GroupType “Security” | Where-Object {$_.DisplayName -eq “Test Security Group”}

Create Object of the member depicted by “UserPrincipalName” parameter that needs to be added to the group

$member = Get-MsolUser -UserPrincipalName spdev001@spdevs001.onmicrosoft.com

Then use the following command to add the member to the group by specifying Group Object ID & Member Object ID

Add-MsolGroupMember -GroupObjectId $securityGroup.ObjectId -GroupMemberType “User” -GroupMemberObjectId $member.ObjectId

12

Once the command gets executed successfully we can see a new member has been added to the group.

We can verify the result of operation by using PowerShell Command

$securityGroup = Get-MsolGroup -GroupType “Security” | Where-Object {$_.DisplayName -eq “Test Security Group”}

Get-MsolGroupMember -GroupObjectId $securityGroup.ObjectId

13

We can also verify the result of operation by Browser as shown below

14

15

How to remove Users from Security Groups

We can we remove the Users from specific security group by using following set of commands

Create the Object of the respective group from which the user needs to be removed

$securityGroup = Get-MsolGroup -GroupType “Security” | Where-Object {$_.DisplayName -eq “Test Security Group”}

Create the object of the respective member that needs to be deleted from the group

$member = Get-MsolUser -UserPrincipalName spdev001@spdevs001.onmicrosoft.com

Then we can use “Remove-MsoLGroupMember” command to remove the member depicted by “Groupmemberobjectid” parameter from the group depicted by “GroupObjectId” parameter as shown below

Remove-MsoLGroupMember -GroupObjectId $securityGroup.ObjectId -GroupMemberType User -Groupmemberobjectid $member.ObjectId

16

Once the command gets executed successfully we can see the respective member gets deleted from the Security Group in question.

Now we can verify the delete operation by using the following PowerShell Command

$securityGroup = Get-MsolGroup -GroupType “Security” | Where-Object {$_.DisplayName -eq “Test Security Group”}

Get-MsolGroupMember -GroupObjectId $securityGroup.ObjectId

17

Or we can verify the same via Browser by Editing the Security Group as shown below

18

19

How to Remove Security Groups

We can use the following command to remove the Security Groups

Create object to the respective group

$securityGroup = Get-MsolGroup -GroupType “Security” | Where-Object {$_.DisplayName -eq “Test Security Group”}

Execute “Remove-MsolGroup” command to remove the group depicted by “objectid” parameter as shown below

Remove-MsolGroup -objectid $securityGroup.ObjectId

On execution this command will ask you for the confirmation on delete action

Enter your choice “Y” to continue “N” to cancel

20

21

Once the command is executed successfully we can go back to Groups section in Admin Center to verify the Delete Action

22

All of the above tasks can be automated by incorporating the CSV files to receive input from and perform the respective actions.

Hope you find it helpful.

Advertisements

2 thoughts on “Office 365: Security Groups Management using Powershell

  1. Thank you. I am trying to remove all members from a provisioning group that I employ in another script. I am not successful. Here is the code. There error I get is “The member you are trying to delete is not in this group.”

    $LicensingGroup = GetMsolGroup -GroupType Security | where {$_.DisplayName -eq “SG_LicenseProvisioning”}
    {
    foreach ($member in $LicensingGroup) {
    GetMsolUser -ObjectId $member.ObjectId
    Remove-MsolGroupMember -GroupObjectId $LicensingGroup.ObjectID -GroupMemberType User -GroupMemberObjectId $member.ObjectId
    }

    Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s