Windows PowerShell


PowerShell and QADUser

PowerShell and QADUser PowerShell QADUser

A company called Quest provides an extra snap-in for PowerShell.  The idea is for these Active Directory cmdlets to work alongside the native PowerShell commands.  As a result we can examine users' properties, and with care, change values and even reset their passwords.

Topics for PowerShell's QADUser

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 QADUser

 get-Command | Where {$_.name -match "QADUser"}

Note 1:  There is a rich seam of verbs that you can apply to QADUser.  You can examine the user with 'get', then configure them with 'set', 'enable' and 'unlock'.  To facilitate a bulk import of users from a spreadsheet there is also, 'new-QADUser'.

Objective:  To Get Information About Active Directory Users

Let us assume that you have fulfilled the above pre-requisites, now there are just two things to do before my scripts will work:

a) Connect to Active Directory, best would be to logon at a domain controller in a test network.  Remote connection works well, and you could try Virtual PC for your test network.

b) Find the variable $OU in my script(s); then amend its value to reflect your domain and your Organizational Unit.  You many need a little extra work with Active Directory Users and Computers in creating an OU and a handful of users.

Example 2: List Users in a Named OU

The key command here is get-QADUser.

# PowerShell script to list Active Directory users in a named OU
# Author: Guy Thomas
# Version 1.4 August 2008 tested on PowerShell v 1.0

$OU = "YourDomName/YourOu"
get-QADUser -SearchRoot $OU

Note 1:   -SearchRoot is the parameter which connects to Active Directory.

Note 2:   You did change the value of $OU - didn't you?  Also Remember that these QAD cmdlets don't exist in the initial PowerShell install, they are only available after you successfully run: add-PSSnapin quest.activeroles.admanagement.  If your script does not work refer back to the pre-requisites.

Example 2a: How to Discover the Names of a User Properties

These QAD cmdlets are designed to fit seamlessly into PowerShell, for example we can apply our trusty interrogation techniques such as, get-help get-QADUser.

# PowerShell script to list a User's Properties
# Author: Guy Thomas
# Version 1.1 August 2008 tested on PowerShell v 1.0

get-Help get-QADUser

Note 1:  I suggest you try my parallel learning technique and match the user properties revealed by QADUser, with the property sheet that you see in Active Directory Users and Computers.

Example 2b: How to List a User's Property with Get-QADUser

As with many of my scripts, there are two learning threads in this example, a real-life objective (Listing user properties) and also learning PowerShell techniques (Piping and word-wrap).

Important Preparation:  Change the value of $OU.  "YourDomName/YourOu" is unlikely to work on your domain, so adjust this value.  Any doubts of the name, consult your Active Directory Users and Computers.

# PowerShell script to list users and their DisplayNames
# Author: Guy Thomas
# Version 1.1 August 2008 tested on PowerShell v 1.0

$OU = "YourDomName/YourOu"
get-QADUser -SearchRoot $OU `
| format-Table FirstName, LastName, DisplayName -auto

Note 1:  The unusual backtick symbol (`) means, wrap the command to the next line.

Note 2:  The pipe symbol (|) is PowerShell's signature tune; it means push the output of the first clause (get-QADUser) into this next command (format-Table).

Challenges: If I were you I would take a timeout to add values to your user's property sheet, e.g. LastName, or DisplayName.

The second part of my challenge is to put into practice what we learned with get-help QADUser, namely to add different fields from my example 2b, for example, Company or Office.  Here is further advice on researching these LDAP properties.

Example 3: Change the Value for the User's Property with Set-QADUser

My objectives here are twofold, firstly, to practice scripting Active Directory in a relatively harmless fashion.  For instance, changing a user's property called 'DisplayName' is less intrusive than changing their password.

Secondly, if we add a text string to displayName then we have a 'handle' to filter Active Directory.  Just to emphasise that the benefit of having a known value for displayName is that we have an extra control to prevent a rogue script changing everybody's password. 

Important Preparation:  As with example 2, you need to edit the this line:
$OU = "YourDomName/YourOu"

# PowerShell script to change a user's display name
# Author: Guy Thomas
# Version 1.1 August 2008 tested on PowerShell v 1.0

$OU = "YourDomName/YourOu"
get-QADUser -SearchRoot $OU `
| set-QADUser -DisplayName "Supremo" `
| FT FirstName, LastName, DisplayName, company, office -auto

Note 1:  Never miss a chance to learn a PowerShell verb; mostly we employ, 'get', but observe that here we also employ the more useful 'set'.

Note 2:  See how I reinforce the idea of piping (|); for example, the output of 'set' becomes the input of FT, which stands for format-Table.

Timeout: Investigate -SearchScope

Before we are ready to experiment with the -SearchScope parameter, I invite you to create a child OU underneath "YourDomName/YourOu".  Next, create few new test accounts in the child OU. 

Amend this line of example 3: get-QADUser -SearchRoot $OU -SearchScope 'OneLevel' `

Next try 'SubTree': get-QADUser -SearchRoot $OU -SearchScope 'SubTree' `

Example 4: QAD Script to Change Passwords

Here is a script which sets the password for users.  The variable $OU specifies the precise location of the user accounts targeted in your domain.

Be aware:  This script has two safety catches.  Firstly, it changes only users with a particular value for DisplayName; secondly I use the -whatIf parameter to test the output.  If the script does as you wish, then remove the last line.

# PowerShell script to change users' passwords
# Author: Guy Thomas
# Version 1.2 August 2008 tested on PowerShell v 1.0

$OU = "cp2.mosel/PowerShell"
get-QADUser -searchRoot $OU -searchScope 'OneLevel' | `
Where-Object {$_.displayName -like "Supremo*"} | `
set-QADUser -userPassword "Lapt00p$" `
-whatIf

Note 1:   set-QADUser has different properties from get-QADUser, for example, 'set' has a property called -userPassword.

Note 2:  As mentioned previously, this script has 'where-Object' clause which acts an extra check that you are changing the users with a particular displayName.  Once you understand how this script works, you could remove the 'where-Object' clause.

  ˚

Example 5:  Changing the Passwords with UserMustChangePassword

Beware:  This script has NO safety catch.  If you prefer, you could begin by appending the -whatIf parameter to the last line, as in the the script above.

Preliminary step, investigate the parameters with the command:
get-help set-QADUser. 

Result of above research:   UserMustChangePassword sounds interesting.  Incidentally, this PowerShell parameters seem much friendlier than the equivalent pwdLastSet and userAccountControl of VBScript.

# PowerShell script to set a user's passwords and force a change at logon
# Author: Guy Thomas
# Version 1.2 August 2008 tested on PowerShell v 1.0

$OU = "cp2.mosel/PowerShell"
get-QADUser -searchRoot $OU -searchScope 'OneLevel' `
Where-Object {$_.description -like "Supremo*"} | `
set-QADUser -userPassword "Lapt00p$" -userMustChangePassword 1

Note 2:   Setting 'userMustChangePassword 1' looks easy, and seems logical enough.  However, I only hit upon this value of numeric one after failing with = "Yes", True, and "1".  You need just plain 1 with no speech marks, and no equals sign.

Note 3:   Observe just how I just appended the -userMustChangePassword parameter.  Did I use a comma?  No.  A semi-colon?  No.  Just straightforward userMustChangePassword 1.

Warning:  If you are not sure of what's happening here, I strongly recommend that you append -whatIf. 

For those who know what they are doing it is possible to create a script which changes all Active Directory accounts.  The secret is to persuade the script to start at the domainRoot/.  The way you achieve this dangerous task is to shorten the line:
$OU = "YourDomName/YourOu", to
$OU = "YourDomName/".  

The result would be a script which could 'get', or 'set' all the accounts.

Summary of PowerShell QADUser

There is a family of QADUser commands each preceded with a different verb.  The two cmdlets that I feature on this page are 'get' and 'set'.   As for learning progression, research how to extract existing properties, then try 'setting' innocuous properties such as DisplayName.  Once you have mastered the basics and stumbled upon the 'whatIf, then you can tackle real tasks such as changing users' passwords.

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

WebThis Site

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

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

Please report a broken link, or an error.