Overview
Sometimes, there is a situation in which we need to connect our PowerShell with Office 365. We received one such requirement from one of our clients. They wanted a CSV for all licensed users available in Office 365. Office 365 does not provide a feature to export all licensed users in .csv format. To generate that, we need to write PowerShell commands.
Prerequisites to run PowerShell command for Office 365
Microsoft Online Services Sign-In Assistant – Download Link
Windows Azure Active Directory Module for Windows PowerShell – Download Link
Installing the above two prerequisites are very easy.
Step 1
After installing the above two prerequisites, we can view “Windows Azure Active Directory Module for Windows PowerShell” in our search window.
The below screenshot shows Windows Azure Active Directory Module for Windows PowerShell.
Step 2
Right-click on Windows Azure Active Directory Module for Windows PowerShell > Run as Administrator.
It will open the below screen.
Step 3
Now, we have to connect Office 365 Account with PowerShell.
To connect Office 365 with PowerShell, use the following line of code.
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
Execute the above two lines of code.
It will prompt a window to ask username and password.
Enter Global Administrator username and password to connect to Office 365 account.
Step 4
After successful login, we are able to see a screen like this.
We can ignore the message in yellow.
Step 5
Now, I want to see all the available users (licensed and unlicensed users). I will use the following code snippet.
Get-MsolUser –All
Step 6
If you only need licensed users, use the following code snippet.
Get-MsolUser -All | where {$_.isLicensed -eq $true}
If you need only unlicensed users, use the following code snippet.
Get-MsolUser -All -UnlicensedUsersOnly
Step 7
Now, we want to export these users to .csv format.
To export licensed users with their account login name, user principal name, and license type, we will use the following code snippet.
Get-MSOLUser -All | where {$_.isLicensed -eq $true} |select DisplayName,userprincipalname,islicensed,{$_.Licenses.AccountSkuId}| Export-CSV c:\O365UserList.csv –NoTypeInformation
Exported CSV file looks like this.
Conclusion
This is how we can export active or licensed users from Office 365 using PowerShell command.
I hope you love this article.
Comments