Schedule Task to Export AD Users to CSV using Powershell script

We can export Active Directory users to CSV file using Powershell cmdlets Get-ADUser and Export-CSV. We have two type of filters, LDAP Filter and SQL Like Filter to filter and select only required users. In this article, I am going to write Powershell script to Export AD Users to CSV file and Steps to create a Schedule Task to Export AD Users to CSV using Powershell script.
 

Summary

Powershell Scripts to Export AD Users to CSV

We can use Active Directory attribute name (property name) to filter users in Get-ADUser cmdlet. The following command export the selected properties of all Active Directory users to CSV file. You can add more attributes as per your wish, refer this article:Get-ADUser Default and Extended Properties to know more supported AD attributes.

Export All Users:

Import-Module ActiveDirectory
Get-ADUser -Filter * -Properties * |
 Select -Property Name,Mail,Department | 
 Export-CSV C:AllADUsers.csv -NoTypeInformation -Encoding UTF8

Export AD Users by Filter:

You can export users only who belongs to Admin Department by applying filter with the AD attribute ‘department‘.

Import-Module ActiveDirectory
Get-ADUser -Filter 'Department -like "*Admin*"' -Properties * |
  Select -Property Name,City,Mail,Department,DistinguishedName | 
  Export-CSV C:AdminUsers.csv -NoTypeInformation -Encoding UTF8

Export AD Users by LDAP Filter:

Instead of SQL Like Filter, you can also use LDAP filter to select only required users. Refer this article (AD LDAP Filter Examples) to get more LDAP filter examples.

Import-Module ActiveDirectory
Get-ADUser -LDAPFilter '(Department=*Admin*)' -Properties * |
  Select -Property Name,City,Mail,Department,DistinguishedName | 
  Export-CSV C:AdminUsers.csv -NoTypeInformation -Encoding UTF8

Export AD Users from specific OU:

We can set target OU scope by using the parameter SearchBase. This following command select all the AD users from the Organization Unit ‘estOU’ and export it to CSV file.

Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "OU=TestOU,DC=TestDomain,DC=Local" -Properties * |
 Select -Property Name,Mail,Department | 
 Export-CSV C:TestOUUsers.csv -NoTypeInformation -Encoding UTF8

Steps to Schedule Powershell script to Export AD Users to CSV

You can create Scheduled Task to run Powershell script using Windows Task Scheduler. Follow the below steps to create daily schedule to export all Admin users from Active Directory to CSV file.

1. Copy the below Powershell script and paste in Notepad file.
2. SaveAs the Notepad file with the extension .ps1 like Export-Admin-Users.ps1

Import-Module ActiveDirectory
Get-ADUser -LDAPFilter '(Department=*Admin*)' -Properties * |
  Select -Property Name,City,Mail,Department,DistinguishedName | 
  Export-CSV C:AdminUsers.csv -NoTypeInformation -Encoding UTF8

Note: You can use your own script file if you already have one.

3. Open the Windows Task Scheduler : Go to > Start > Administrative Tools and select Task Scheduler.

Schedule Task to Export AD Users to CSV using Powershell script

4. In the Task Scheduler, select the Create Task… option under the Actions menu.

Schedule Task to Export AD Users to CSV using Powershell script

5. Enter a name for the task, and give it a description (the description is optional and not required).
6. Under Security options section, you can specify different user account that the task should be run under and select the option ‘Run whether user logged on or not‘ so that the task will run even if the user is not logged.

Schedule Task to Export AD Users to CSV using Powershell script

7. Then, select the Triggers tab, and click New to add a new trigger for the scheduled task. This new task should use the On a schedule option. The start date can be set to a desired time, and the frequency and duration of the task can be set based on your specific needs and click OK. Here, I have configured Daily schedule to Export AD Admin users to CSV file on daily basis.

Export AD Users to CSV via Schedule Task using Powershell script

8. Then, go to the Actions tab and click New to set the action for this task to run. Set the Action to Start a program.
9. In the Program/script box enter Powershell
10. In the Add arguments (optional) box enter the complete script file path. For example, if your Powershell Script is named “Export-Admin-Users.ps1” and placed under “C:Scripts“. then you have to enter path like: “C:ScriptsExport-Admin-Users.ps1

Export Active Directory Users to CSV via Schedule Task using Powershell script

11. That’s all, we completed the new schedule task configuration and click OK to complete process.

Export Active Directory Users to CSV via Task Scheduler using Powershell script

12. Under Task Scheduler Library, You can check daily task run status of your task and you can also run the task whenever you want by right-click on the task and click Run.

Export Active Directory Users to CSV via Task Scheduler using Powershell script

Advertisement

2 thoughts on “Schedule Task to Export AD Users to CSV using Powershell script”

  1. I have a computer AD Export scheduled every week similar to this however the task scheduler runs and sends the csv out in an email but the information contained in the sent CSV never updates unless I manually login to the server.

    Reply

Leave a Comment