The M Code Behind the Power Query M function ActiveDirectory.Domains

What is Active Directory?

Active Directory (AD) is a Microsoft directory service that allows businesses to manage user and computer accounts, security groups, and other resources from a central location. It is a hierarchical database that stores information about objects on a network, such as users, groups, computers, and printers. Active Directory is commonly used in enterprise environments to manage permissions, authentication, and network resources.

The ActiveDirectory.Domains M Function

The ActiveDirectory.Domains function is a Power Query M function that allows users to retrieve a list of all domains in an Active Directory forest. This function is incredibly useful for administrators who need to manage multiple domains and want to automate the process of retrieving information about them.


The syntax for the ActiveDirectory.Domains function is as follows:

ActiveDirectory.Domains(server as text, optional options as nullable record) as table

The function takes two parameters: the server name and an options record. The server parameter is a text value that specifies the name of the domain controller to connect to. The options record is an optional parameter that allows users to specify additional options for the connection, such as the authentication method to use.


Here is an example of how to use the ActiveDirectory.Domains function:


Source = ActiveDirectory.Domains("dc01.mydomain.local"),

#"Expanded domains" = Table.ExpandTableColumn(Source, "domains", {"distinguishedName", "name", "netBiosName", "dnsRoot"}, {"distinguishedName", "name", "netBiosName", "dnsRoot"})


#"Expanded domains"

This code retrieves a list of all domains in the Active Directory forest hosted by the domain controller named dc01.mydomain.local. The resulting table contains four columns: distinguishedName, name, netBiosName, and dnsRoot, which are the attributes of the domain objects.

How the ActiveDirectory.Domains Function Works

The ActiveDirectory.Domains function uses the System.DirectoryServices.ActiveDirectory .NET Framework class to connect to Active Directory and retrieve information about the domains in the forest. This class provides access to the Active Directory Service Interfaces (ADSI), which allows users to perform directory service tasks such as querying and modifying objects.

The ActiveDirectory.Domains function creates a new domain context object using the server name parameter and the options record. It then retrieves a list of all domains in the forest using the EnumerateDomains method of the domain context object. The resulting list is transformed into a table and returned to the user.

Power Query is a powerful tool for data transformation and shaping, and the ActiveDirectory.Domains function is an excellent example of its capabilities. With this function, administrators can quickly and easily retrieve a list of all domains in an Active Directory forest and use it for further analysis. The M code behind the ActiveDirectory.Domains function is a great example of how powerful M can be for data transformation and analysis.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)