PowerShell Export-CsvPowerShell's Export-Csv with Get-QADUserThis page is where PowerShell meets CSVDE, any knowledge of CSVDE and its switches is helpful in understanding how to use export-Csv. Even if your ultimate goal is to bulk-import accounts into Active Directory, there are sound reasons to master export-Csv first. For instance, exporting user objects is safer than importing new accounts. One bonus with both import and export-Csv is that, you learn about the LDAP properties of the Active Directory objects. Our learning progression, is to master get-QADuser, then export the output into a .csv file. Topics for PowerShell Export-Csv
♣ Pre-requisites, particularly for the QAD snap-inBefore we can get my examples to work you need to meet these pre-requisites.
Example 1: Getting to know PowerShell's export-Csv cmdlet# Research PowerShell's export-Csv Note 1: The key conclusion of this basic research is that export-Csv requires a path. When we employ this cmdlet for real, we must include a parameter to tell PowerShell where to store the output. Preparing the stream of users with get-QADUserLet us sort out the $Variables. I like using variables and they are particularly useful in my examples, because they draw attention to the particular values that you must edit in order that my modified script works on your system. Where are your users! Mine are in "OU=YourPeople,DC=cp5,DC=mosel", therefore change this line in your script. OU obviously means Organizational Unit, but DC means Domain Context, and not domain controller. You can find the correct domain name in your Active Directory Users and Computers. # PowerShell script to list users in a named OU Note 1: Another useful feature of get-QADUser is the parameter -searchRoot. This works somewhat like dir /s, in that with -searchRoot the cmdlet 'get-QADUser' drills down into child OUs. Challenge 1: You could substitute -searchScope 'OneLevel' for -searchRoot. Other values for -searchScope are 'Base' and 'SubTree'. If you need more information try, help get-QADUser -full Example 2: Combining QADUser with export-CsvWhere should your .csv be created? Mine will appear magically at "c: \YourDir\people2.csv", however, you need to edit the $FilePath variable in this script. Naturally, you also need to amend the value for $OuDomain, as you did in the previous example.
# PowerShell cmdlet to export active directory into a csv file Note 1: One of PowerShell's most useful constructions is the pipe (|). This enables the output of get-QADUser to become the input of export-Csv. Note 2: While you can read the .csv file in notepad, I find that a spreadsheet such as Excel much more convenient for displaying the columns of data. Example 3: Filtering users' properties with select-ObjectDid you suffer from information overload in the previous example? If so, you can reduce the number of LDAP fields by adding a search-Object command which lists just the properties you are interested in, each separated by a comma. Actually, the previous example provides an excellent chance to research the users' properties by studying the first two rows of the exported .csv file.
# PowerShell cmdlet to export active directory into a csv file Note 1: The tiny backtick ` symbol tells PowerShell that the command continues on the next line. In other words, (`) means word-wrap. Note 2: Here is further advice on researching these LDAP properties
Guy Recommends: SolarWinds LANSurveyor
|
||||||
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2009 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |