Deep Dive Into Office 365 PowerShell Cmdlets (Part 8)

by [Published on 1 Feb. 2017 / Last Updated on 1 Feb. 2017]

In the part 7 of this article series, we gave an overview of different types of operations performed by the Office 365 PowerShell cmdlets. By using the Office 365 PowerShell cmdlets, you can perform operations such as Collect, Create, Remove and Modify. As stated in in Part 7, Microsoft PowerShell developers follow a standard naming convention for most of the PowerShell cmdlets that have been designed so far. For create operations, most of the PowerShell cmdlets start with “New” or “Add” word. For PowerShell cmdlets that can be used to modify an object start with the word “Edit” or “Set” and for PowerShell cmdlets that perform a remove operation might have a “Remove” word in it.

If you would like to read the other parts in this article series please go to:



In Part 7, we also explained Get-MsolGroup PowerShell cmdlet that you can use to retrieve the list of groups created in an Office 365 tenant and Get-MsolGroupMember PowerShell cmdlet to retrieve the member of the Office 365 groups. In this article, we will continue to explain a PowerShell script that you can use to provide a summary of Office 365 groups and another PowerShell script that will be useful to know as to how many Office 365 groups have been created without adding a description text. Both the PowerShell scripts, explained in this article, use Get-MsolGroup PowerShell cmdlet. To use both the PowerShell scripts, run it from a computer that have Windows Azure Active Directory Module for Windows PowerShell installed. It is assumed that you have connected to an Office 365 tenant using an account that has permissions to gather the data from Office 365 WAAD.


Reporting Office 365 Groups Summary

In case you need to report on the number of Office 365 groups created in an Office 365 tenant, you can use below PowerShell script. Below PowerShell script collects the number of Distribution List groups, the number of security groups, number of MainEnabledSecurity groups and the number of groups that have been synced from On-Premise Active Directory.
### Start Script ###

$Result = "C:\Temp\Result.CSV"
IF (Test-Path $Result)
{
    Remove-item $Result
}
$STR = "Item, Value"
Add-Content $Result $STR

$Error.Clear()
$AllGroups = Get-Msolgroup -All
IF ($Error.Count -eq 0)
{
    $TotDistGroups = ($AllGroups | Where-Object {$_.GroupType -eq 'DistributionList'}).Count
    $TotSecGroups = ($AllGroups | Where-Object {$_.GroupType -eq 'Security'}).Count
    $TotMailGroups = ($AllGroups | Where-Object {$_.GroupType -eq 'MailEnabledSecurity'}).Count
    $AllGroupsSynced = Get-Msolgroup -MaxResults 200 | Where-Object {$_.LastDirSyncTime –ne $null}

    Write-Host "Total Distribution Groups: " $TotDistGroups
    Write-Host "Total Security Groups: " $TotSecGroups
    Write-Host "Total MailEnabledSecurity Groups: " $TotMailGroups
    Write-Host "Total Groups Synced from On-Premise Active Directory: " $AllGroupsSynced

    $STR = "Number of Distribution Groups:, "+$TotDistGroups
    Add-Content $Result $STR
    $STR = "Number of Security Groups:, "+$TotSecGroups
    Add-Content $Result $STR
    $STR = "Number of Mail Enabled Security Groups:, "+$TotMailGroups
    Add-Content $Result $STR
    $STR = "Number of Groups Synced from On-Premises:, "+$AllGroupsSynced
    Add-Content $Result $STR
}
else
{
    Write-Host "Some Errors occurred running Get-MsolGroup command"
}
Write-Host "Results saved in $Result"

### End Script ###

Once the script has finished executing, you will see a report file by name Result.CSV in C:\Temp folder. The report file looks like as shown in the screenshot below:

Image

Reporting Office 365 Groups without Description

When you create an Office 365 Group, you are asked to provide a description for the group. A Description field is provided for you to explain the purpose of creating the Office 365 group. While the Description field is optional, but it plays an important role when you have thousands of Office 365 groups created in an Office 365 Tenant and you don’t know why you created these groups. You can use below PowerShell script to get a list of Office 365 groups that do not have description set.
### Start Script ###

$Result = "C:\Temp\GroupsWithoutDescription.CSV"
IF (Test-Path $Result)
{
    Remove-item $Result
}
$STR = "Item, Value, Group Type"
Add-Content $Result $STR

$Error.Clear()
$AllGroups = Get-Msolgroup –All | Where-Object {$_.Description –eq $null}
IF ($Error.Count -eq 0)
{
    $TotDistGroups = ($AllGroups | Where-Object {$_.GroupType -eq 'DistributionList'}).Count
    $TotSecGroups = ($AllGroups | Where-Object {$_.GroupType -eq 'Security'}).Count
    $TotMailGroups = ($AllGroups | Where-Object {$_.GroupType -eq 'MailEnabledSecurity'}).Count

    Write-Host "Total Distribution Groups without Description: " $TotDistGroups
    Write-Host "Total Security Groups without Description: " $TotSecGroups
    Write-Host "Total MailEnabledSecurity Groups without Description: " $TotMailGroups

    $STR = "Number of Distribution Groups without Description:, "+$TotDistGroups
    Add-Content $Result $STR
    $STR = "Number of Security Groups without Description:, "+$TotSecGroups
    Add-Content $Result $STR
    $STR = "Number of Mail Enabled Security Groups without Description:, "+$TotMailGroups
    Add-Content $Result $STR

    ForEach ($allGroupsNow in $AllGroups)
    {
        $STRNew = ","+$allGroupsNow.DisplayName+","+$allGroupsNow.GroupType
        Add-Content $Result $STRNew
    }

}
else
{
    Write-Host "Some Errors occurred running Get-MsolGroup command"
}
Write-Host "Results saved in $Result"

### End Script ###

As you can see in the script above, we are using “$AllGroups = Get-Msolgroup | Where-Object {$_.Description –eq $null}” PowerShell command to get all Office 365 groups and then filter groups that do not have any text entered in the description field. Once the Script has finished executing, you will see the result in C:\Temp\GroupsWithoutDescription.CSV file. The report includes the number of security groups and the group name that does not have a description set as shown in the screenshot below:

Image


 Summary

In this part, we explained two PowerShell scripts that you can use to report a summary of Office 365 groups created in an Office 365 tenant and report on Office 365 groups that do not have a description text set. As you might have noticed in both the PowerShell scripts above, the scripts use Get-MsolGroup PowerShell cmdlet to collect the required information.
In the next part of this article series, we will provide more examples of using Office 365 Groups PowerShell cmdlet.  We will also provide a PowerShell script that you can use to report on the health status of Office 365 groups that sync from On-Premises Active Directory.

See Also


The Author — Nirmal Sharma

Nirmal Sharma avatar

Nirmal Sharma is a MCSEx3, MCITP and was awarded the Microsoft MVP award in Directory Services and Windows Networking. He specializes in Microsoft Azure, Office 365, Directory Services, Failover Clusters, Hyper-V, PowerShell Scripting and System Center products. Nirmal has been involved with Microsoft Technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to PowerShell-based Dynamic Packs for www.ITDynamicPacks.Net solutions.