Title: Licence assignment O365 to Azure AD
Tags: powershell;office365;azure-powershell
Question: I'm here to ask you a question about my code, when I try to give a licence to my user there's an error that told me the user cannot be found.
But If I launch the following one, alone it works.
$upn = 'carrollmichael@example.com'
Connect-MsolService
#Get-MsolUser | Where-Object {($_.licenses).AccountSkuId -match &quot;reseller-account:SPB&quot;}
Get-MsolUser -UserPrincipalName $upn
Set-MsolUser -UserPrincipalName $upn -UsageLocation US
Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses &quot;reseller-account:SPB&quot;
But the same one, in my code at the end of user creation there it is, it seem that he can't take the string  :
Get-MsolUser : User Not Found.  User: carrollmichael@example.com. Au caractère C:\Users\...\OneDrive - ...\Bureau\AzureAD_user.ps1:47 : 1
+ Get-MsolUser -UserPrincipalName $upn
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [Get-MsolUser], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Admini stration.Automation.GetUser
Set-MsolUser : User Not Found.  User: . Au caractère C:\Users\...\OneDrive - ...\Bureau\AzureAD_user.ps1:48 : 1
+ Set-MsolUser -UserPrincipalName $upn -UsageLocation US
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [Set-MsolUser], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Admini stration.Automation.SetUser
Set-MsolUserLicense : User Not Found.  User: . Au caractère C:\Users\...\OneDrive - ...\Bureau\AzureAD_user.ps1:49 : 1
+ Set-MsolUserLicense -UserPrincipalName $upn -AddLicenses &quot;reseller-ac ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [Set-MsolUserLicense], MicrosoftOnlineException
+ FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.UserNotFoundException,Microsoft.Online.Admini stration.Automation.SetUserLicense
Any advices ? I'm a begginner and want to give to my users o365 licences right after the user creations. :D
Comment: Works perfectly, I just added a :Start-Sleep -Seconds 10
Thanks to you ! :D

Comment: Could it be a timing issue? i.e. if you create a new user account, it may take a little bit of time (e.g. several seconds) before you can `Get-MsolUser` and find them. Try adding a delay before you try to `Get-MsolUser` and see if that works.


Here is another answer: It could be a timing issue.
If you create a new user account, it takes a little bit of time (e.g. it could be several seconds) before you can use Get-MsolUser and find them. Try adding a delay before you try to Get-MsolUser and see if that works. e.g.
New-MsolUser ....
Start-Sleep -Seconds 10
Get-MsolUser ....

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:08/08/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<powershell><office365><azure-powershell>', 'Title': 'Licence assignment O365 to Azure AD', 'ViewCount': 38, 'id': 73279439, 'user_AboutMe': None, 'user_AccountId': 26009574.0, 'user_DisplayName': 'Atross', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 19718862.0}}