Home » Dsquery » Use Dsquery to Export Group Members to CSV

Use Dsquery to Export Group Members to CSV

Use the Dsquery Group command to find the group based on provided samid and pipe it to Dsget group command to get group members and export group members to csv file.

This article will discuss how to use Dsquery to export group members to a CSV file.

Export Group Members to CSV using Dsquery

Follow the below steps to get group members and export them to a CSV file.

Step 1- Open Command Prompt (Run as Administrator)

Go to Start >> Open the cmd (Command prompt) with the Run As Administrator option.

Command Prompt ( Run as Administrator)
Command Prompt ( Run as Administrator)

Step 2 – Use the Dsquery to Export Group Members to CSV file

Use the Dsquery group command to get the group based on specified search criteria.

Refer to the following code to get the group using the dsquery group command and pipe it to the dsget group command to get group members.

dsquery group -samid "SALESLEADER" | dsget group -members

In the above command, the dsquery group command finds the group based on the specified samid and pipes it to the dsget group command. It uses the members parameter to get all members of the ad group.

The output of the above command to get adgroup members using dsquery and dsget commands are:

Dsquery to get group members
Dsquery to get group members

Step 3 – Export Group Members to CSV

To export group members to CSV retrieved using the dsquery group command, pipe the output of the dsget group -members command to the path.

Refer to the following code which takes all group members and export group members to a CSV file.

dsquery group -samid "SALESLEADER" | dsget group -members > C:\PowerShell\groupmembers.csv

The output of the export group members to a CSV file is:

Dsquery Export Group Members to CSV
Dsquery Export Group Members to CSV

Cool Tip: How to find domain controllers using cmd!

Conclusion

I hope the above article on how to export active directory group members to a CSV file using the dsquery group command is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.