Edit Office 365 Groups with PowerShell

One of my clients asked me to write a script to change the name, primary SMTP address and description of Office 365 Groups. I remembered that Wictor Wilen wrote a blog series about this subject.

To meet my client's needs, I need to use the Set-UnifiedGroup PowerShell cmdlet to change those Office 365 Groups properties. I'll use the script Wictor uses to connect to Exchange Online, which you can see in figure 1. I have modified Wictor's script (highlighted in the box) so it can process an XML file with a batch of groups in there. In line 10, I import the XML file and then I will declare a variable in the right node in our XML:

Set-Location $PSScriptRoot  
[xml]$xmlfile = Get-Content ".\XML\Groups.xml"

$Groups = $xmlfile.Groups.Group

Next, I added a foreach loop that uses the XML file to loop through all the groups in that XML file. We need to do some error handling, so that is why I also added a try-catch-finally block in the foreach loop. So, in Wictor's original script, I replace line 18 with the following block of code to do this:

foreach ($Group in $Groups) {  
  try {
    # Set Group
    Set-UnifiedGroup -Identity $Group.Identity `
                     -DisplayName $Group.DisplayName ` 
                     -MailTip $Group.MailTip `
                     -PrimarySmtpAddress $Group.PrimarySmtpAddress
    Write-Host "Group changed to:" `
                $Group.Identity "/" `
                $Group.DisplayName "/" `
                $Group.MailTip "/" `
                $Group.PrimarySmtpAddress -ForegroundColor Green
  }
  catch {
    Write-Host "Oops! Something went wrong with the following group:" `
                $Group.DisplayName -ForegroundColor Red
  }
  finally {
    Write-Host "End of script"
  }
}

I only use the Identity, DisplayName, MailTip and PrimarySmtpAddress parameters in this script, but you can add other parameters if you want. To see all the files I made during this blogpost, check out my GitHub repository.

Last but not least: thanks to Wictor for the great posts about Office 365 Groups!

Comments

comments powered by Disqus