Microsoft 365 E3 User Check

Brass Contributor

Hello I am trying to adjust a script I have that checks if a user is licensed for E3 in Microsoft 365. The script I have below works by checking a list of users and seeing which ones are licensed for E3. However it does not tell me the users in that list that are NOT E3 licensed. The .csv output looks like this....


My question is can I have the "IsLicensed" column say "False" if the user does not have the E3 license? Currently it just skips it and does not include it in the output. The script I am working with now is below.

Get-content c:\temp\users.txt | foreach {Get-MsolUser -UserPrincipalName $_ | Where-Object {($_.licenses).AccountSkuId -match "SPE_E3"}} | Select-Object UserPrincipalname,islicensed | Export-csv c:\temp\E3.csv

Any help is GREATLY appreciated I am on a timeline new to Powershell and cant find an answer in Google .



3 Replies
best response confirmed by charlie4872 (Brass Contributor)

Well you are basically filtering all the users that do have the license, so everyone else is skipped. If you want to list the users that do not have the license, adjust the statement in the where clause to -notmatch. If you want both licensed and unlicensed, remove the clause altogether and simply populate the IsLicensed value accordingly:


Get-content c:\temp\users.txt  | foreach { Get-MsolUser -UserPrincipalName $_ } | Select-Object UserPrincipalname,@{n="islicensed";e={(&{if ($_.Licenses.AccountSkuId -match "tenant:ENTERPRISEPACK") {"True"} else {"False"}})}}| Export-csv c:\temp\E3.csv

 where I've complicated things a bit by doing everything in-line, but hopefully you get the idea. Make sure you enter the correct SKUid.

Thanks for the reply @Vasil Michev For some reason running the example you gave me and adjusting the SKUid exports a .csv and all users have the Islicensed column populated False. I then made a users.txt file with half of the list known F1 users and half known E3 users and the .csv output comes back with all users showing False in the Islicensed column. Any ideas what might be happening? And thanks again for your help!


Hard to tell without specific examples, but then again I didn't spend much time testing the example :)