Using PowerShell to export Active Directory information
By: Douglas Correa | Updated: 2016-02-22 | Comments (1) | Related: More > PowerShell
You need to export the information from Active Directory in automated way, one way to do that is using PowerShell.
With PowerShell it's possible to export the data to 3 types of files (TXT, CSV and XML).
To get Active Directory information using PowerShell, first, it's necessary to install the PowerShell module into the server. Open Server Manager, select Features and select "Add Features" then navigate as shown below and select "Active Directory module for Windows PowerShell".
Next, run the command Import-Module in PowerShell.
I've tried to export the information into CSV and TXT, but some data would truncate or change characters, so I choose XML and that works very well. To export the XML file just put | Export-Clixml after the command below along with a file name to capture the data. I just selected 5 fields to export, but Active Directory has many more attributes.
The result file:
To automate the execution, open Notepad, copy and paste the below commands and save the file as C:\ADInfo.ps1 extension.
Import-Module ActiveDirectory; Get-ADUser -Filter * -Properties GivenName,Surname,Title,Department,City | Export-Clixml R:\Ad.xml;
The Windows Task Scheduler is used to run the script with a date and time configured. You can see below the program used is PowerShell and the argument is -file along with the script file and path.
If you try to run the task it may show an error like "cannot be loaded because running scripts is disabled on this system". To run the script without error, you need to set the execution policy to RemoteSigned, run the command Set-ExecutionPolicy with administrator rights.
- Look for the next tip on how to read Active Directory using T-SQL.
- More information about the command Get-ADUser
- Active Directory attributes reference
- Using the Set-ExecutionPolicy Cmdlet
- Check out these other PowerShell tips
Last Updated: 2016-02-22
About the author
View all my tips