Use this script to check user group and move user to new group. The typical use case is: when you want to assign licensing based on group without assigning it dynamically.

Technical Details

This PowerShell script performs the following actions:

  • It retrieves the members of the Active Directory group named “O365_E1_License” using the cmdlet Get-ADGroupMember.
  • For each member returned by Get-ADGroupMember, it executes the following commands within a foreach loop:
    1. It adds the current member to the Active Directory group “O365_E5_License” using the cmdlet Add-ADGroupMember.
    2. It removes the current member from the Active Directory group “O365_E1_License” using the cmdlet Remove-ADGroupMember.
  • It then performs a similar set of actions for the Active Directory groups “O365_E3_License” and “O365_E5_License”:
    1. It retrieves the members of the group “O365_E3_License” using Get-ADGroupMember.
    2. For each member, it adds them to the group “O365_E5_License” and removes them from the group “O365_E3_License” using the respective Add-ADGroupMember and Remove-ADGroupMember cmdlets.

In summary, the script transfers members from “O365_E1_License” to “O365_E5_License” and members from “O365_E3_License” to “O365_E5_License” in Active Directory.

The Script

Copy the script below or download the txt file.

Get-ADGroupMember "O365_E1_License" | ForEach-Object {
Add-ADGroupMember -Identity "O365_E5_License" -Members $_
Remove-ADGroupMember -Identity "O365_E1_License" -Members $_
}
Get-ADGroupMember "O365_E3_License" | ForEach-Object {
Add-ADGroupMember -Identity "O365_E5_License" -Members $_
Remove-ADGroupMember -Identity "O365_E3_License" -Members $_
}

********************

Change the name “O365_E1_License” to the correct name of your group.
Change the name “O365_E3_License” to the correct name of your group.