Windows PowerShell


PowerShell Export-Csv

PowerShell's Export-Csv with Get-QADUser

This 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-in

Before we can get my examples to work you need to meet these pre-requisites.

  1. Download and install PowerShell and .Net Framework. 
    Go to Microsoft's site and choose the flavour to suit your operating system.
  2. Download, then install the QAD Snap-ins from this site:
    http://www.quest.com/activeroles-server/arms.aspx
  3. Register the snap-in. (Key point)
    add-PSSnapin quest.activeroles.admanagement
  4. Gain access to Active Directory.  Best would be to logon at a domain controller in a test domain.

Example 1: Getting to know PowerShell's export-Csv cmdlet

# Research PowerShell's export-Csv
get-Help export-Csv -full

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-QADUser

Let 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
$OuDomain = "OU=YourPeople,DC=cp5,DC=mosel"
get-QADUser -searchRoot $OuDomain

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-Csv

Where 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
$FilePath = "c:\YourDir\people2.csv"
$OuDomain = "OU=YourPlace,DC=cp5,DC=mosel"
get-QADUser -searchRoot $OuDomain | export-Csv $FilePath

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-Object

Did 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
$FilePath = "c:\YourDir\people3.csv"
$OuDomain = "OU=YourPlace,DC=cp5,DC=mosel"
get-QADUser -searchRoot $OuDomain `
| select-Object name, SamAccountName, UserPrincipalName `
| export-Csv $FilePath

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 LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

Summary of PowerShell's Export-Csv

Export-Csv comes into its own when you want to save information about Active Directory users.  It is also a safe way of preparing for import-Csv, which in turn, is a convenient way of creating a large number of users, computers, or groups from a spreadsheet.

See more PowerShell QAD Scripts

PowerShell Home   • Quest QAD   • QADUser   • QAD Password   • QADGroup   • QADComputer

 • Export-CSV   • Import CSV

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.