PowerShell Workflow
$E1strGroup1 = "E1 group"; 
$E1group = Get-ADGroup -Identity $E1strGroup1 -Properties SamAccountName | Select-Object SamAccountName -ExpandProperty SamAccountName 
$Groups1 = "Mention Group names from where user should be removed" #This is for Adding users to E1 with Skype group# 
Write-Host "Assigning license to the users based on the criteria" 
#Assigning license to the users based on the criteria# 
$SQLServer = "sqlservername"  
$SQLDBName = "tablename" 
$SqlQuery = "query which will get the data" 
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"  
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
$SqlCmd.CommandText = $SqlQuery 
$SqlCmd.Connection = $SqlConnection 
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
$SqlAdapter.SelectCommand = $SqlCmd 
$DataSet = New-Object System.Data.DataSet 
$DataSet.Tables[0] | Export-Csv "C:\Test\userdata-$Date-$hour-$Min.csv" -NoTypeInformation 
Start-Sleep -Seconds 3 
$QueryUser = Import-Csv -Path "C:\Test\userdata-$Date-$hour-$Min.csv" 
$lanid = $QueryUser.lanid 
foreach($us in $lanid){ 
Write-Host "Working on User - $us" 
$Account = Get-ADUser -Filter {SamAccountName -like $us} -Properties msExchRecipientTypeDetails | Select -ExpandProperty msExchRecipientTypeDetails 
if($Account -eq '2147483648'){ 
$mems = Get-ADPrincipalGroupMembership –Identity $us | Select-Object -ExpandProperty name 
if ($mems -contains $E1group) { 
              Write-Host "$us Already exists in the group - $E1group" 
              "$(Get-Date) $us Already exists in the group - $E1group" >> "C:\Test\userdata-$Date\$fnames+$(Get-Date -format M.d.yyyy).txt" 
              Write-Host "$us is added to - $E1group" 
              Add-ADGroupMember -Identity "$E1group" -Members "$us" -Confirm:0 -server corp.mphasis.com -ea stop 
              trap {$err = ("[ERROR] $us - " + $_); $err >>"C:\Test\userdata+$(Get-Date -format M.d.yyyy).txt"; Continue } 
            "$(Get-Date) $us - is added to - $E1group " >>"C:\Test\userdata-+$(Get-Date -format M.d.yyyy).txt" 
    foreach ($Groups in $Group1){ 
        if($mems -contains $Group1){ 
               Write-host "$us - is getting Removed from other Groups " -ForegroundColor Green 
               Remove-ADGroupMember -Identity $Groups -Member $us -Confirm:$False 
               trap {$err = ("[ERROR] $us - " + $_); $err >>"C:\Test\userdata+$(Get-Date -format M.d.yyyy).txt"; Continue } 
               "$(Get-Date)  $us - is getting Removed  $Groups " >>"C:\Test\userdata+$(Get-Date -format M.d.yyyy).txt" 
if($Account -eq '1'){ 
Write-Host "$us is not Added to $E1group as mailbox is still in on-Prem" 
"$us - is not Added to $E1group as mailbox is still in on-Prem">>"C:\Test\userdata+$(Get-Date -format M.d.yyyy).txt" 



Prerequisite for this Script is to make sure you have assigned O365 License to the Group which you will be using, for assigning O365 License. You can manage this Group by Adding or removing the members.

The script will get the Data from your DB where your employee information saved and then it will check whether the employee/user is part of the Group on which you have assigned the license or not. IF the user is part of other groups then it will REMOVE the user from that Group and ADD him to the stated Group.