In this article, I have included loads of blog examples on how to connect to your Office 365 account and retrieve account details like license name, service plan, licensed users, and external users. You have to follow the below instructions for better results.
- Connect to AzureAD
- PowerShell code
Connect to AzureAD
Run Windows PowerShell as an administrator.
In that command window, run "Install-Module -Name AzureAD" to install the AzureAD, if it is not installed on your end. If it shows a message about installing a module from an untrusted repository, then type "Y" and press the Enter button.
Then run the command "Connect-AzureAD". It will prompt you to sign in to your Microsoft account. Type your Office 365 account user name and password and click Next.
Then run the command "Install-Module MSOnline" to install MSOnline on your end. If it prompts you to install the NuGet provider, type ‘Y’. If it prompts you to install the module from PSGallery, type ‘Y’ and press ENTER.
Run the "Connect-MsolService" command and then provide your credentials and click on next.
PowerShell Code
The below code retrieves the details of license name, licensed users, external users, and services plan of your Office365 Account.
- $credential = Get - Credential
- Connect - MsolService - Credential $credential
- try {
- #Retrieve license list present inside tenant
- $license = Get - MsolAccountSku
- for ($i = 0; $i - lt $license.Count; $i++) {
- $nameLicense = $licenseLists[$i]
- #Retrieve License Name
- Write - Host "License Name ="
- $nameLicense.SkuPartNumber
- #Retrieve assigned users count
- Write - Host "Assigned Users ="
- $nameLicense.ConsumedUnits
- }
- $extUserCount = Get - MsolAccountSku | Where - Object {
- $_UserPrincipalName - like "*#EXT#*"
- }
- Write - Host "External User = "
- $extUserCount.Count
- #Show the service status of Tenant
- Get - MsolAccountSku | Select - ExpandProperty ServiceStatus
- sleep 10
- } catch {}
The out-put will be like the below image.