Retrieving Information from Computers Belonging to an Active Directory OU

This script queries a particular Organizational Unit (OU) in your Active Directory for a list of computers. It will then use WMI to query various properties of those computers and output it into table format. Take a look at the script Listing 3-2.

$ou = [ADSI]"LDAP://ou=test,dc=testlab,dc=local"
$computers = $ou.PSBase.Get_Children()
$arrInfo = @()
foreach($node in $computers){
$arrInfo += Get-WmiObject - query "Select `
	Name,Manufacturer,Model, `
	NumberOf Processors, `
	TotalPhysicalMemory `
	From Win32_ComputerSystem" `
	-computername &node.Name
$arrInfo | format-table Name,Manufacturer, `
	Model, NumberOf Processors, TotalPhysicalMemory

Listing 3-2

As you can see in Figure 3-2, this script lists the name, manufacturer, model, number of processors, and the total amount of physical memory for each computer in the given OU. For now, I'm assuming that every item in the given OU is a computer. In a real production script, I would put in a lot more error checking between commands and also create filters for the Active Directory query because OUs can contain computers and other types of objects such as users, groups, and contacts.

If you want to see how this script runs on your own system, open your text editor and type it in (or use the file for this listing on the book's Web site). You have to change the LDAP path to an existing OU within your Active Directory, then save it as complist.psl. Now run this script and watch the magic happen.

Tip: You'll see that some lines end with a backtick (`) character. This means that the next line is just a continuation of the current line and not a separate command. This is useful if you have very long commands that you want to break up into multiple lines rather than have them keep going on and on to the right.

Warning: Don't confuse the backtick (`) with the single quote ('). The backtick character is typically found to the left of the number 1 key on U.S. keyboard (usually above the tab key).

This is an excerpt from:

Windows PowerShell 2 is the scripting language that enables automation within the Windows operating system. Packed with powerful new features, this latest version is complex, and Windows PowerShell 2 For Dummies is the perfect guide to help system administrators get up to speed. Written by a Microsoft MVP with direct access to the program managers and developers, this book covers every new feature of Windows PowerShell 2 in a friendly, easy-to-follow format.

Windows PowerShell 2 is the updated scripting language that enables system administrators to automate Windows operating systems
System administrators with limited scripting experience will find this book helps them learn the fundamentals of Windows PowerShell 2 quickly and easily
Translates the jargon and complex syntax of Windows PowerShell 2
Covers script debugging improvements, the ability to invoke commands remotely, and the new user interface
Uses real-world applications to clarify the theory, fundamentals, and techniques of the scripting language

Windows PowerShell 2 For Dummies makes this tool easily accessible to administrators of every experience level.

Learn more at

More Windows Administration Information:
• Disable Kernal Paging to Speed Up Windows
• DOS Switches and Wild Cards
• How to Map a Network Folder in Windows 7
• How to Erase a Hard Disk Drive Permanently
• Disable Cutesy Effects to Speed Up Windows 10
• Turn Off Windows 10 Snap and Shake
• How to Create a Bootable USB Drive
• The Different Types of Virtualization
• WSH to Master Your Computer
• What is the difference between MSI and EXE Installations?