Exchange custom attributes on groups automatically with PowerShell

Scenario:

Set Exchange custom attributes on groups automatically with PowerShell.

In general the below script add an Exchange custom attribute on distribution and security groups. I am working on a custom delegation model on all our systems so the local IT Admins will have the least possible permissions to perform their work without any interruption. Mail enabled distribution and security groups are included in the list of the objects in Microsoft Exchange to be delegated. Based on the delegation model that we want to achieve, we need to utilise an exchange custom attribute so we will be able to perform filtering on Exchange Online (Office365).

As all changes in a hybrid environment need to be done on Exchange On-premise and then synchronised to Office 365, the script first connects on Exchange On-premise server. The next step is to define the Organizational Units in Active Directory for each country in variables and also retrieve all mail enabled groups in our system from Exchange and save them in another variable. After that the script will proceed with some checks so it will know if it will proceed with a change or not on the exchange customer attributes. If the attributes does not much, in this case CustomAttribute4, or the value in null then it will proceed to set the custom attribute to the one specified.

As you will see, I am also changing two more exchange attributes before I change the exchange custom attribute. The first attribute, MemberJoinRestriction, does not allow anyone to join without manager of the group to add the member or approve the join request. The second attributes, MemberDepartRestriction, does not allow members to leave from the group if without group manager to approve it, the group manager to remove the member

You will see that I use ForceUpgrade parameter when I setting up the custom attribute. The reason I am using this parameter is to upgrade the groups created on previous versions of exchange and when you perform change on them it will ask you to upgrade them. With this parameter it will upgrade them and the perform the change.

All the changes that will be performed, they will be sent to me so I will be able to know what has been done. If there are no changes, no email will be sent. Error report is also configured at the end of the script so I there is any error during the run it will inform me and send me the errors that appeared. I use the below script on a schedule basis and performs the changes automatically daily.

You can download the script here or copy it from below.

Let me know your thoughts and comment below if you need anything.

Related Links:

Solution / Script: