HI,
Can you try this suggestion and check if achieve your need?
Will keep checking the license status of the users until all users are licensed.
If there are still unlicensed users after each check, it will wait for 60 seconds before checking again.
# If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
$LicensedUsers = Import-Csv D:\Users.csv | Select UserPrincipalName
# Initialize a variable to keep track of unlicensed users
$unlicensedUsers = $LicensedUsers
# While there are unlicensed users, keep checking their license status
while ($unlicensedUsers) {
# Create a new array to hold any users that are still unlicensed
$stillUnlicensed = @()
foreach ($user in $unlicensedUsers) {
$licenseStatus = Get-MsolUser -UserPrincipalName $user.UserPrincipalName | select -ExpandProperty isLicensed
# If the user is not licensed, add them to the still unlicensed array
if ($licenseStatus -eq $false) {
$stillUnlicensed += $user
}
}
# Replace the unlicensed users array with the still unlicensed array
$unlicensedUsers = $stillUnlicensed
# If there are still unlicensed users, wait for a while before checking again
if ($unlicensedUsers) {
Start-Sleep -Seconds 60
}
}