Office 365: Working with Groups using PowerShell (I)!

One of the latest additions to Office 365 Groups is the availability of new PowerShell commands that facilitate to do CRUD operations with Groups, add members to a Froup, etc. For example, in the PowerShell Script included as part of this post you can see how to use the Get-UnifiedGroup, New-UnifiedGroup, Set-UnifiedGroup and Remove-UnifiedGroup cmdlets to perform CRUD operations with Groups is made. The script, which you can download from here How to do CRUD operations with Office 365 Groups, is the following one:

############################################################################################################################################

# Script that allows to do work with Office 365 Groups using standard cmdlets for Groups

# Required Parameters: N/A

############################################################################################################################################

 

#Connection to Office 365

$msolCred = Get-Credential

Connect-MsolService -Credential $msolCred

 

#Definition of the function tthat allows to do work with Office 365 Groups using standard cmdlets for Groups

function WorkWith-Office365Groups

{

    param ($sOperationType,$sGroupName,$sNewGroupName)       

    try

    {

        switch ($sOperationType) 

        { 

        "Read" {

            Write-Host "Get all the Office 365 Groups in a tenant" -ForegroundColor Green                        

            Get-UnifiedGroup

            } 

        "Create" {

            Write-Host "Creating a new Office 365 Group" -ForegroundColor Green                 

            New-UnifiedGroup –DisplayName $sGroupName

            }

        "Update" {

            Write-Host "Updating an Office 365 Group" -ForegroundColor Green                 

            #The change in the name can be seen in the O365 Admin Portal

            Set-UnifiedGroup -Identity $sGroupName -DisplayName $sNewGroupName

            } 

        "Remove" {

            Write-Host "Removing an Office 365 Group" -ForegroundColor Green     

            Remove-UnifiedGroup -Identity $sGroupName

            }           

        default {

            Write-Host "Requested Operation not valid!!" -ForegroundColor DarkBlue            

            }

        }

 

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()

    }

}

 

Write-Host "-----------------------------------------------------------"  -foregroundcolor Green

Write-Host "Working with Groups through PowerShell." -foregroundcolor Green

Write-Host "-----------------------------------------------------------"  -foregroundcolor Green

 

$sOperationType="Read"

$sGroupName="O365 PowerShell Group"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sGroupName

$sOperationType="Create"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sGroupName

$sOperationType="Update"

$sNewGroupName="Test PS"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sGroupName -sNewGroupName $sNewGroupName

$sOperationType="Remove"

$sNewGroupName="Test PS"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sNewGroupName

Once you execute the script in the PowerShell ISE, you will get the following output:

image

Furthermore, when the group is created, an email to the account creating the Group is sent to notifying the user that he is a member of the Group.

image

Nota: In order to use new Office 365 Groups cmdlets, I recommend you to read the following Corey Roth’s post: http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2015/05/12/working-with-office-365-groups-using-powershell.aspx

Office 365: Trabajo con Grupos por medio de PowerShell (I)!

Una de las últimas novedades incorporadas por Microsoft a los Grupos de Office 365 es la disponibilidad de nuevos comandos PowerShell que faciliten realizar operaciones CRUD con Grupos, añadir miembros a un Grupo, etc. A modo de ejemplo, os comparto un script PowerShell en el que se hace uso de los comendos Get-UnifiedGroup, New-UnifiedGroup, Set-UnifiedGroup y Remove-UnifiedGroup para realizar operaciones CRUD con Grupos. El script, que podéis descargaros desde aquí How to do CRUD operations with Office 365 Groups, es el siguiente:

############################################################################################################################################

# Script that allows to do work with Office 365 Groups using standard cmdlets for Groups

# Required Parameters: N/A

############################################################################################################################################

 

#Connection to Office 365

$msolCred = Get-Credential

Connect-MsolService -Credential $msolCred

 

#Definition of the function tthat allows to do work with Office 365 Groups using standard cmdlets for Groups

function WorkWith-Office365Groups

{

    param ($sOperationType,$sGroupName,$sNewGroupName)       

    try

    {

        switch ($sOperationType) 

        { 

        "Read" {

            Write-Host "Get all the Office 365 Groups in a tenant" -ForegroundColor Green                        

            Get-UnifiedGroup

            } 

        "Create" {

            Write-Host "Creating a new Office 365 Group" -ForegroundColor Green                 

            New-UnifiedGroup –DisplayName $sGroupName

            }

        "Update" {

            Write-Host "Updating an Office 365 Group" -ForegroundColor Green                 

            #The change in the name can be seen in the O365 Admin Portal

            Set-UnifiedGroup -Identity $sGroupName -DisplayName $sNewGroupName

            } 

        "Remove" {

            Write-Host "Removing an Office 365 Group" -ForegroundColor Green     

            Remove-UnifiedGroup -Identity $sGroupName

            }           

        default {

            Write-Host "Requested Operation not valid!!" -ForegroundColor DarkBlue            

            }

        }

 

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()

    }

}

 

Write-Host "-----------------------------------------------------------"  -foregroundcolor Green

Write-Host "Working with Groups through PowerShell." -foregroundcolor Green

Write-Host "-----------------------------------------------------------"  -foregroundcolor Green

 

$sOperationType="Read"

$sGroupName="O365 PowerShell Group"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sGroupName

$sOperationType="Create"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sGroupName

$sOperationType="Update"

$sNewGroupName="Test PS"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sGroupName -sNewGroupName $sNewGroupName

$sOperationType="Remove"

$sNewGroupName="Test PS"

WorkWith-Office365Groups -sOperationType $sOperationType -sGroupName $sNewGroupName

La salida por pantalla correspondiente es la siguiente:

image

Además, cuando se crea el Grupo, se recibirá un correo electrónico en el buzón de la cuenta de Office 365 utilizada para su creación indicando que es miembro del Grupo.

image

Nota: Para poder usar los nuevos cmdlets para Grupos de Office 365, os recomiendo leer el siguiente artículo de Corey Roth: http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2015/05/12/working-with-office-365-groups-using-powershell.aspx