Share via


Office 365: Get A List Of All Your Customers

Before you start...

This article is a part of a collection of articles.  Please read Office 365 Reporting via PowerShell for Syndication Partners to understand the audience this article is intended for, the background, what's in scope and out of scope, assumptions and limitations.

Scenario

The Syndication Partner needs to get a list of all its Office 365 customers regularly. By taking snapshots regularly of existing customers, the Syndication Partner will then compare any two dates to track changes on the list of customers. Only extracting data using PowerShell is covered in this article.  Comparison of data between two dates is not covered.

Get Report for All Customers 

Input

None. The script will retrieve information for all customer tenants of the partner

Output

One CSV file located in C:\My Office 365 Reports named:

  • CustomerList.csv => List of all customers (regardless of whether they have a subscription or not) including Tenant ID and onmicrosoft domain.****

 

Note

The script will create the folder C:\My Office 365 Reports if it does not exist already Sample Script

 

# This script is meant to be used with Syndication Partner Administrator credentials# Output report named "CustomerList.csv" will be date stamped and saved at C:\My Office 365 ReportsClear# Prepare output file$reportTime = Get-Date$timeStamp = ($reportTime).tostring("yyyyMMdd_HHmmss ")[IO.Directory]::CreateDirectory("C:\My Office 365 Reports")$outputFile = "C:\My Office 365 Reports\" + $timeStamp + " CustomerList.csv" $columnLabels = "Report Start Date,Syndication Partner,TenantID,Initial Domain"Out-File -FilePath $outputFile -InputObject $columnLabels -Encoding UTF8# Get Syndication Partner name$PartnerName = Get-MsolPartnerInformation$MyPartnerName = $PartnerName.PartnerCompanyName# Get list of all customers$tenants = Get-MsolPartnerContract -All$i=0$tenants | ForEach-Object { $i++ Write-Host "Processing " $i " of " $tenants.count  # Get customer information $currentTenantID = $_.TenantID $currentCompany = Get-MsolCompanyInformation -TenantId $currentTenantID # Exclude information of the Syndication Partner or Op-Cos from Customer List if ($currentCompany.CompanyType -eq "CompanyTenant")  { # Put customer information into variables $myTenant = $currentTenantID $myInitialDomain = $currentCompany.InitialDomain # Write customer record into a CSV file $currentRecord = "$reportTime,$myPartnerName,$myTenant,$myInitialDomain" Out-File -FilePath $outputFile -InputObject $currentRecord -Encoding UTF8 -Append }}

Sample Output

Click image for better quality CustomerList.csv

Other Languages

This article is also available in the following languages: