Get all Licensed Office 365 users using PowerShell

Sometimes, we might need to view all the Licensed Users within a Office365 tenant. So here, I am going to demonstrate how to get all the users for any further analysis.

But before we delve into this plz make sure that you have all the components installed on the machine to run SharePoint Online Management Shell and  the cmdlet, Connect-MsolService. You can refer this blog post also for any assistance: https://realmpksharepoint.wordpress.com/2014/07/10/install-sharepoint-online-management-shell-on-client-side/

Following are the steps involve to perform this task:

  • Open the SharePoint Online Mangement Shell in Administrator mode.
  • Type in Connect-MsolService. When the window pops up, enter your corresponding administrator credentials.
  • If no error message is thrown then your credentials are validated.
  • In case you’re interested in getting all the available properties for the user object returned by the Get-MsolUser, then type in the cmdlet,
    Get-MsolUser | Get-MsolMember | Out-GridView
    

    The following screen will be displayed.

  • As you can see that there’s a property called isLicensed, we’re now gonna use this property in our next cmdlet to get all the Licensed Office365 users only, by running the command,
    Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" }
    

  • Finally, you can also export this information to a csv file. To do this, run this command,
    Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | Select-Object UserPrincipalName, DisplayName, Country, Department, ValidationStatus | Export-Csv c:\LicensedUsers\LicensedUsers.csv
    

  • You can also view all the properties by running the command,
    Get-MsolUser | Where-Object { $_.isLicensed -eq "TRUE" } | Export-Csv c:\LicensedUsers\LicensedUsers.csv
    

The last 2 commands will not generate any message on the shell, but you can check the file at your given location.

Thanks…

Add a reference of the Microsoft.Online.Administration.Automation.PSModule dll to your Project in Visual Studio

First of all make sure that all the required items are already installed on your machine. You can view the requirements here, https://realmpksharepoint.wordpress.com/2014/07/10/install-sharepoint-online-management-shell-on-client-side/. The Microsoft.Online.Administration.Automation.PSModule dll is required to execute the
Connect-MsolService

command and get the return values in the form of type, Microsoft.Online.Administration.User from within the .net C# code. Well, it’s supposed to be a simple job of adding a reference of this dll to your project. The problem is to identify the location of this dll.

Initially, I did some googling to download the dll. However, I couldn’t find any trusted source (Microsoft) from where I can download this dll. Then, I realized that this dll should have been downloaded with the installation of Windows Azure Active Directory Module. Hence, I navigated to that location I found the dll at the following location

C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline

Unfortunately, the job’s not done yet. A strange thing started happening here. Though, I can view this dll in my Explorer. The folder, MSOnline was inaccessible from the Visual Studio Add Reference window!

Still, I am not aware of this weird behavior. What I did to resolve this issue is, I copied the entire MSOnline folder to the bin directory of the project and from there I was able to add the dll’s reference to the project.

Install SharePoint Online Management Shell on Client-Side

This particular job caused quite a bit of trouble. Though, the objective was just to download and install the SharePoint Online Management Shell, the issues arose due to various dependencies. I have tried to list down the various steps involved in this operation below

System Requirements:

    • Supported Operating System.
      • Windows 7 Service Pack 1,
      • Windows 8,
      • Windows Server 2008 R2 SP1,
      • Windows Server 2008 Service Pack 2,
      • Windows Server 2012
  • PowerShell 3.0

Update PowerShell from 2.0 to 3.0

If you’re running the PowerShell version < 3.0 then you need to update it to 3.0.  You can check the version of the PowerShell by running the following command in the shell.

$PSVersionTable.PSVersion

The ScreenShot above displays a PowerShell of version 3.0. You may get version 2.0 or if you can also get an error message stating, that the variable does not exist then, it is safe to assume that the engine is version 1.0. If your engine is 3.0 or higher you can skip this segment.

Next, download the latest Windows Update from the url, http://www.microsoft.com/en-us/download/details.aspx?id=34595 and install it.

Install Instructions:
To install Windows Management Framework 3.0:
1.    Download the correct package for your operating system and architecture.

  • Windows 7 Service Pack 1
  • 64-bit versions: Windows6.1-KB2506143-x64.msu
  • 32-bit versions: Windows6.1-KB2506143-x86.msu
  • Windows Server 2008 R2 SP1
  • 64-bit versions: Windows6.1-KB2506143-x64.msu
  • Windows Server 2008 Service Pack 2
  • 64-bit versions: Windows6.0-KB2506146-x64.msu
  • 32-bit versions: Windows6.0-KB2506146-x86.msu

2.    Close all Windows PowerShell windows.
3.    Uninstall any other versions of Windows Management Framework 3.0.
4.    Run the MSU file that you downloaded.
For information about troubleshooting the installation, see the Release Notes.

To uninstall Windows Management Framework 3.0:
5.    In Control Panel/Programs/Uninstall a program/View installed updates, locate and uninstall the following installed Windows Update:

  • KB2506143 – for Windows 7 SP1 and Windows Server 2008 R2 SP1
  • KB2506146 – for Windows Server 2008 SP2

SharePoint Online Management Shell

Download and install SharePoint Online Management Shell from the url, http://www.microsoft.com/en-us/download/details.aspx?id=35588 

Run the Shell

Open the SharePoint Online Management Shell

And type the command Connect-MsolService and press enter. If everything is fine then, you’ll be shown the following screen

Otherwise, the following error message will be displayed!

Fix the Issue to run Connect-MsolService, PowerShell command

If you can run the command, Connect-MsolService, then everything is fine and you can skip this step and run your commands successfully.

To successfully run the Connect-MsolService PowerShell command, you need to:
1.    Find out what bitness your operating system is (x86 / 32-bit OR x64 / 64-bit). See Microsoft’s “Is my PC running the 32-bit or 64-bit version of Windows?” article for help.
2.    If necessary, install the appropriate bitness version of PowerShell 3.0 (which I hope we just did already)
Note 1: Windows6.0 is Windows Vista and Windows6.1 is Windows 7
Note 2: If you get a message of “The update is not applicable to your computer.” during the install then either you downloaded the wrong version of you already have it installed
3.    Install the appropriate bitness version of the Microsoft Online Services Sign-In Assistant for IT Professionals
4.    Install the appropriate bitness version of the Windows Azure Active Directory Module for Windows PowerShell
Note: If you get an error of  “In order to install Windows Azure Active Directory Module for Windows PowerShell, you must have Microsoft Online Services Sign-In Assistant version 7.0 or greater installed on this computer.” and a resulting failed install, install the Microsoft Online Services Sign-In Assistant for IT Professionals BETA (you shouldn’t need to uninstall the normal version but I would recommend it).
5.    Run the appropriate bitness version of PowerShell
6.    Run the Import-Module MSOnline PowerShell command
7.    Finally, run the Connect-MsolService PowerShell command

You should now be able to see the login screen.
 

Finally, it’s done!!

[Edit]
It has been noted that on some machines, even though the command runs successfully directly from the PowerShell, the same cmdlet throws the “cmdlet not supported” error when trying to execute it from within a C# application. To tackle it:

Copy the folders called MSOnline and MSOnline Extended from
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\ 

to the folder
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\

And then in PS run the Import-Module MSOnline, and it will automatically get the modules.