How to Export or Import AD (Active Directory) Group Members

Photo of author

By Victor Ashiedu

Published

Do you want to export or import AD members (users) from Active Directory groups? This guide teaches you different methods to perform this task.

Option 1: Manage AD Group Members with PowerShell

As I already hinted in my introduction, you can use PowerShell to either export or import users from an Active Directory group. Talking about importing or exporting users, PowerShell can import from a CSV file or export to a CSV file.

However, when it comes to a text file, although you can import group members from a text file, it is limited. So, in the second subsection of this section, I will only show you how to export group members to a text file.

1. Export AD Group Members to a Text or CSV File With PowerShell

Follow the steps below to export or import group members to a text or CSV file:

  1. Decide the user’s properties you want to export. To get a list of all the properties of the users in an AD group, use the command below:
To run the command, change “Remote Writers” to the name of the Active Group you want to export its members.
Get-ADGroupMember -Identity "Remote Writers"

The command returns the default properties of the members of the AD group. The AD group in my sample command has two members.

In this example, I’ll export the group members’ name, SamAccountName, and distinguishedName. Now, I will modify the previous command to return these values.

Get-ADGroupMember -Identity "Remote Writers" | Select-Object name, SamAccountName, distinguishedName

As expected, this command returns the properties I specified…

How To Export AD Group Members To A Text Or CSV File With PowerShell
  1. Decide whether you want to use the default properties of the headers or customize them. The next important step to exporting members of an AD group is to decide whether you want to change the table headers or use the default values.

If you decide to use the default values, retain the last command (as shown below); then proceed to step 3.

Get-ADGroupMember -Identity "Remote Writers" | Select-Object name, SamAccountName, distinguishedName

However, if you decide to customize the result headers, use a command similar to the one below:

Get-ADGroupMember -Identity "Remote Writers" | Select-Object @{Name="Full Name";Expression={$_.name}}, @{Name="UserName";Expression={$_.SamAccountName}}, distinguishedName
The modified column names uses the format – @{Name=”Column_name”;Expression={“object_property”}}. The Column_name is the name you want to display in the result. However, the object_property is the property name of the object – expressed with the pipeline variable – $_

Now the command returns more user-friendly column names!.

  1. Finally, export the AD group members to a CSV file or text file with the following commands, respectively.
Get-ADGroupMember -Identity "Remote Writers" | Select-Object @{Name="Full Name";Expression={$_.name}}, @{Name="UserName";Expression={$_.SamAccountName}}, distinguishedName | Export-CSV "C:\report\Remote_Writers_Members.CSV" -NoTypeInformation
The difference between this command and the last command is that, this command pipes the last command to the command – Export-CSV “C:\report\Remote_Writers_Members.CSV” -NoTypeInformation. I included the NoTypeInformation parameter of the Export-CSV command to remove typeinfomation from the CSV header.
Get-ADGroupMember -Identity "Remote Writers" | Select-Object @{Name="Full Name";Expression={$_.name}}, @{Name="UserName";Expression={$_.SamAccountName}}, distinguishedName | Out-File "C:\report\Remote_Writers_Members.txt" 

The two commands exports the AD group members to a CSV and text file respectively. In the screenshot below, I show the files explored and also the text file.

How To Export AD Group Members To A Text Or CSV File With PowerShell

2. Import Users to Active Directory Groups From CSV with PowerShell

In the last sub-section, I showed you how to export members of an AD group to text or CSV file with PowerShell. In this sub-section, you’ll learn how to perform the opposite task – import Active Directory users from a CSV file as AD group members.

To perform this task, follow the steps below:

  1. Create a CSV file with the SamAccountName of the AD users. The CSV file will have the SamAccountNames of the users you want to add to the AD group.

You can use the command below to export the SamAccountNames of the users. In this example, I am exporting all users in the domain to a CSV file called All_AD_users.csv.

In the command, I filtered out administrator and guest accounts. You can change the -SearchBase parameter value to the DN of another AD object you want to search for and export users from.
Get-ADUser -Filter "(Name -notlike 'guest') -and (Name -notlike 'administrator')" -SearchBase "DC=itechguides,DC=local" | Select-Object SamAccountName | Export-Csv C:\report\All_AD_users.csv -NoTypeInformation
  1. Import the users to the AD group using the command below. In this example, I am adding the AD users I exported in step 1 to the “Remote Writers” Active Directory group.

As shown in the screenshot below, the “Remote Writers” AD group does not have any members.

How To Import Users To Active Directory Groups From CSV With PowerShell

To import and add all AD users in my CSV file to the “Remote Writers” Active Directory group, I’ll run the command below:

Import-Csv C:\report\All_AD_users.csv | ForEach-Object {Add-ADGroupMember -Identity "Remote Writers" -Members $_.SamAccountName}

After I run the command, the “Remote Writers” Active Directory group has all the users in the CSV file as members.

Option 2: Manage AD Group Members with DSQuery

In this section, you’ll learn how to export AD group members to a text file. Unfortunately, if you use the “dsquery group” command to export users to a CSV file, it does not create headers.

So, if you want to use the command prompt to export Active Directory group members, I recommend you export to a text file. Based on all the aforementioned, to export the members of the “Remote Writers” Active Directory group to a text file, I’ll run the command below:

dsquery group -name "Remote Writers" | dsget group -members > C:\report\All_AD_users_.txt
The command uses the “dsquery group” command to return the DN of the AD group. Then, it pipes the result to the “dsget group” command, then uses its -members parameter to export the members. Finally, I the command export the users to a text file using the > redirection operator.

The result is a text file with the DN of all the users in the AD group.

Frequently Asked Questions

1. How do I export and import users from Active Directory groups?

To export users from an Active Directory group, run the Get-ADGroupMember PowerShell command. For example, the command below exports all members of the “Remote Writers” AD group:

Get-ADGroupMember -Identity “Remote Writers” | Select-Object SamAccountName | Export-CSV c:\reports\remote_writers.CSV -NoTypeInformation

Then, I can import the exported users as members of another AD group with the command below:

Import-Csv c:\reports\remote_writers.CSV | ForEach-Object {Add-ADGroupMember -Identity “Remote Writers” -Members $_.SamAccountName}

2. How do I export users from Active Directory?

There are so many objects you can export users from in AD. For example, you can export all users in the domain.

Alternatively, you can export users from a group or an OU. To export all users in your domain to a text file, run a command like the one below:

Get-ADUser -Filter “(Name -notlike ‘guest’) -and (Name -notlike ‘administrator’)” -SearchBase “DC=itechguides,DC=local” | Select-Object SamAccountName | Export-Csv C:\report\All_AD_users.csv -NoTypeInformation

Replace the value in the -SearchBase parameter with the DN of your domain. However, if you want to export all members of an OU, add the DN of the OU in the -SearchBase parameter.

Finally, to export AD users from a group, see question 1 above.

3. How do I export data from Active Directory to Excel?

To export data from Active Directory to Excel:

i) Run the relevant PowerShell command to retrieve the data you need from Active Directory
For example, to get the SamAccountName of all AD users in the domain (excluding guest and administrator), run the command below:

Get-ADUser -Filter “(Name -notlike ‘guest’) -and (Name -notlike ‘administrator’)” -SearchBase “DC=itechguides,DC=local” | Select-Object SamAccountName

ii) Then, to export the result to CSV file, pipe the result of the above command to the Export-Csv command. Here is a sample command.

Get-ADUser -Filter “(Name -notlike ‘guest’) -and (Name -notlike ‘administrator’)” -SearchBase “DC=itechguides,DC=local” | Select-Object SamAccountName | Export-Csv C:\report\All_AD_users.csv -NoTypeInformation

Before you run this command, change the -SearchBase to the DN of your domain.

4. How do I create an Active Directory user from a CSV file?

To create an Active Directory user from a CSV file, follow the steps below:

i) Use the Import-CSV command to fetch the users in the CSV file.
ii) Then, pipe the output of the Import-CSV command to a ForEach-Object command.
iii) Finally, within the ForEach-Object command, run the New-ADUser command and specify the entries from the CSV file.

Here is a sample command that creates new AD users from a CSV file that contains the users’ names and SamAccountNames.

Import-CSV [path to CSV file] | ForEach-Object {New-ADUser -Name $_.SamAccountName}

In this example, I assume that the user names of the users are in the SamAccountName column of the CSV file.

5. How do I export users from Windows?

The fastest way to export users from Windows is to run the Get-LocalUser PowerShell command. If you run this command without any parameters, it displays all Windows users on the local computer on the PowerShell console.

However, to export the users to a text file, pipe the result of the Get-LocalUser to Out-File.

Get-LocalUser | Out-File [path to text file]

Alternatively, to export all local Windows users to a CSV file, run the command below:

Get-LocalUser | Export-CSV [path to text file]NoTypeInformation

Conclusion

One of the requested AD SysAdmins receives is producing user reports. Interestingly, one of the popular requests AD SysAdmins get is exporting members of an AD group.

The good news is that in this guide, I covered all the methods to complete this task.

Specifically, in the guide, I showed you how to perform this task with PowerShell. Not only that, but you also learned how to import Active Directory users to AD group members.

Then, the guide also covered how to export AD group members with Command Prompt.

I hope I made your day with this guide! If I did, click on “Yes” beside the “Was this page helpful” question below.

You may also express your thoughts and opinions by using the “Leave a Comment” form at the bottom of this page.

Finally, to keep improving your Active Directory skills, read more guides on our Active Directory Guides page.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment

Send this to a friend