Script to loop through a CSV file and to exit loop once all users have been licensed in M365

mark terry 85 Reputation points
2024-06-14T21:24:03.0433333+00:00

Hi all,

I have an input CSV file like this:

UserPrincipalName

[email protected]

[email protected]

When I license these users in M365, it can take a couple of moments for the Get-MSolUser "IsLicensed" attribute to be set to "True".

I would like to have a PowerShell script so that when it is run, it will check to see when the users have been licensed. The script requirements would be:

  • Read the contents of the CSV file.
  • Create a loop so that the script will check if each user is licensed i.e. "Get-MsolUser -UserPrincipalName [email protected] | select IsLicensed" and the "IsLicensed" value is equal to True
  • Once all users in the CSV file have been confirmed that they have been licensed, exit the loop.

What I have right now, is a foreach iteration which will read in my input file and check if the MSOLusers are licensed or not i.e.

$LicensedUsers = Import-Csv D:\Users.csv | Select UserPrincipalName

foreach ($user in $LicensedUsers)

{

*Get-MsolUser -UserPrincipalName $User.UserPrincipalName | select isLicensed*

}

The above will return the isLicensed value for each user in the CSV. I think I need a "wrapper" loop to check on the status of the Get-MsolUser -UserPrincipalName $User.UserPrincipalName | select isLicensed command and to keep looping until all users in the CSV file are licensed, but don't know how to incorporate the while loop part!

Thanks in advance!

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,630 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,247 questions
Microsoft Exchange Licensing
Microsoft Exchange Licensing
Microsoft Exchange: Microsoft messaging and collaboration software.Licensing: Rules, regulations, and restrictions that define how software can be used and distributed.
23 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Miguel Gonçalves 961 Reputation points
    2024-06-14T22:21:21.8+00:00

    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
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.